C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ replace_if()

C++ 算法replace_if()

C++ 算法 replace_if()函数用于将new_value分配给[first,last)范围内的所有元素对于哪个谓词谓词返回true。
此函数检查范围内的每个元素,如果满足指定谓词,则将其替换。

语法

template <class ForwardIterator, class UnaryPredicate, class T>
void replace_if (ForwardIterator first, ForwardIterator last,
UnaryPredicate pred, const T& new_value );

参数

first: 前向迭代器,指向要替换元素的范围内的初始位置。
last: 前向迭代器,指向要替换元素的范围内的最终位置。
pred : 一元谓词函数必须
new_value : 分配给其旧值满足谓词的元素的新值。

返回值

复杂度

在首尾之间的距离内,线性是线性的。

数据竞争

[first1,last1)范围内的对象将被访问并可能被修改。

异常安全

如果任何pred,元素分配或迭代器上的操作引发异常,则此函数将引发异常。
请注意,无效参数会导致未定义的行为。

示例1

让我们看一个简单的示例来演示replace_if()的用法:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  vector<int> v = { 3,1,2,1,2 };
  replace_if(v.begin(), v.end(),
    [](int x) { return x%2 != 0; }, 10);
  for_each(v.begin(), v.end(),
    [](int x) { cout << x << ","; });
    
    return 0;
}
输出:
10,10,2,10,2,
上面的示例从 Vector v确定奇数,并将所有找到的元素替换为10、

示例2

让我们看另一个简单的示例:
#include <vector>  
#include <algorithm>  
#include <iostream>  
  
bool greater6 ( int value ) {  
   return value >6;  
}  
  
int main( ) {  
   using namespace std;  
   vector <int> v1;  
   vector <int>::iterator Iter1;  
  
   int i;  
   for ( i = 0 ; i <= 9 ; i++ )  
      v1.push_back( i );  
  
   int ii;  
   for ( ii = 0 ; ii <= 3 ; ii++ )  
      v1.push_back( 7 );  
  
   random_shuffle ( v1.begin( ), v1.end( ) );  
   cout << "The original vector v1 is:\n ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
  
   // Replace elements satisfying the predicate greater6  
   // with a value of 70  
   replace_if ( v1.begin( ), v1.end( ), greater6 , 70);  
  
   cout << "The vector v1 with a value 70 replacing those\n "  
        << "elements satisfying the greater6 predicate is:\n ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
   
   return 0;
}
输出:
The original vector v1 is:
 ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 ).
The vector v1 with a value 70 replacing those
 elements satisfying the greater6 predicate is:
 ( 4 70 70 70 0 5 70 1 6 70 3 70 70 2 ).
在上面的示例中, Vector v1的值70替换了满足更大6个谓词的那些元素。

示例3

让我们来看另一个简单的示例:
#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
bool isDivisibleByThree
(
  int n //in
)
{
  return (n%3 == 0);
}
 
int main()
{
  int a[] = {1, 2, 2, 3, 4, 5, 2, 6};
  vector<int> v(a, a+8);
 
  cout <<"\nHere are the values in the vector:\n";
  for (vector<int>::size_type i=0; i<v.size(); i++)
    cout <<v.at(i)<<" ";
 
  cout <<"\nNow we replace all values divisble by 3 with 123.";
  replace_if(v.begin(), v.end(), isDivisibleByThree, 123);
 
  cout <<"\nHere are the revised contents of the vector:\n";
  for (vector<int>::size_type i=0; i<v.size(); i++)
    cout <<v.at(i)<<" ";
 
  return 0;
}
输出:
Here are the values in the vector:
1 2 2 3 4 5 2 6 
Now we replace all values divisible by 3 with 123.
Here are the revised contents of the vector:
1 2 2 123 4 5 2 123  

示例4

让我们看另一个简单的示例:
#include <iostream>     // std::cout
#include <algorithm>    // std::replace_if
#include <vector>       // std::vector
using namespace std;
bool IsOdd (int i) { return ((i%2)==1); }
int main () {
  vector<int> myvector;
  // set some values:
  for (int i=1; i<10; i++) myvector.push_back(i);               // 1 2 3 4 5 6 7 8 9
  replace_if (myvector.begin(), myvector.end(), IsOdd, 0); // 0 2 0 4 0 6 0 8 0
  cout << "myvector contains:";
  for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';
  return 0;
}
输出:
myvector contains: 0 2 0 4 0 6 0 8 0

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4