替换算法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
posted @ 2022-07-31 16:24  thomas_blog  阅读(36)  评论(0编辑  收藏  举报