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