C++遍历算法transform

transform(sourceBeg,sourceEnd,destBeg,op)
sourceBeg:源容器开始迭代器
sourceEnd:源容器结束迭代器
destBeg:目标容器开始迭代器
op:函数或者函数对象
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

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

class Transform 
{
public:
   int operator()(int i)
   {
      return i + 100;
   }
};

int main()
{
   vector<int> v;
   v.push_back(1);
   v.push_back(5);
   v.push_back(3);

   vector<int> vTarget;
   vTarget.resize(v.size());

   transform(v.begin(), v.end(), vTarget.begin(), Transform());
   for_each(vTarget.begin(), vTarget.end(), Print());

   return 0;
}
$ ./a.out          
101
105
103
posted @ 2022-07-31 11:56  thomas_blog  阅读(39)  评论(0编辑  收藏  举报