替换算法replace

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

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

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

   replace(v.begin(), v.end(), 3, 30);
   for_each(v.begin(), v.end(), Print());
   
   return 0;
}
$ ./a.out 
0
1
2
30
4
posted @ 2022-07-31 16:16  thomas_blog  阅读(23)  评论(0编辑  收藏  举报