替换算法replace_if

2022/8/1 14:54:55

本文主要是介绍替换算法replace_if,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Print
{
public:
   void operator()(int i)
   {
      cout << i << endl;
   }
};

class Replace
{
public:
   bool operator()(int i)
   {
      return i >= 3;
   }
};

int main()
{
   vector<int> v;
   for(int i = 0; i < 5; i++)
   {
      v.push_back(i);
   }

   replace_if(v.begin(), v.end(), Replace(), 30);
   for_each(v.begin(), v.end(), Print());
   
   return 0;
}
$ ./a.out           
0
1
2
30
30


这篇关于替换算法replace_if的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程