C++ 算法 中的 replace_if函数

首页 / C++入门教程 / C++ 算法 中的 replace_if函数

C++算法 replace_if()函数用于为pred谓词返回true的[first,last)参数内的所有元素分配new_value。

此函数检查参数内的每个元素,如果满足指定谓词,则将其替换。

replace_if - 语法

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

replace_if - 参数

first:一个正向迭代器,它指向要替换元素的参数内的初始位置。

last:一个正向迭代器,指向要替换元素的参数内的最终位置。

pred :必须满足的一元谓词函数是要替换的元素的值。

new_value :分配给其旧值满足谓词的元素的新值。

replace_if - 返回值

没有

replace_if - 例子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,

上面的示例根据向量v确定奇数,并将所有找到的元素替换为10。

replace_if - 例子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 ).

在上面的示例中,向量v1的值70替换了满足更大6个谓词的那些元素。

replace_if - 例子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  

replace_if - 例子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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

程序员的数学基础课 -〔黄申〕

重学前端 -〔程劭非(winter)〕

编辑训练营 -〔总编室〕

安全攻防技能30讲 -〔何为舟〕

深入浅出云计算 -〔何恺铎〕

系统性能调优必知必会 -〔陶辉〕

分布式系统案例课 -〔杨波〕

李智慧 · 高并发架构实战课 -〔李智慧〕

云时代的JVM原理与实战 -〔康杨〕

好记忆不如烂笔头。留下您的足迹吧 :)