代码如下:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;void print01(int val)
{cout << val << " ";
}class print02
{
public:void operator()(int val){cout << val << " ";}
};void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}for_each(v.begin(), v.end(), print01);cout << endl;for_each(v.begin(), v.end(), print02());cout << endl;}int main()
{test01();return 0;
}
测试结果:
总结:
代码如下:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;class TransForm
{
public:int operator()(int val){return val;}
};class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}vector<int>vTarget;vTarget.resize(v.size());transform(v.begin(), v.end(), vTarget.begin(), TransForm());for_each(vTarget.begin(), vTarget.end(), MyPrint());}int main()
{test01();return 0;
}
测试结果:
总结: