transform容器拷备
class TransForm { public: int operator()(int val) { return val; } }; void test04() { vector<int> v; for (int i = 0; i < 10; i++) v.push_back(i); vector<int> vTar; //transform不会给目标容器分配内存,所以需要我们提前分配好内存 vTar.resize(v.size()); transform(v.begin(), v.end(), vTar.begin(), TransForm()); for_each(vTar.begin(), vTar.end(), [](int v) { cout << v << endl; }); } //将两个容器搬到一个容器里 class TransForm2 { public: int operator()(int val,int val2) { return val+val2; } }; void test05() { vector<int> v; vector<int> v2; for (int i = 0; i < 10; i++) { v.push_back(i); v2.push_back(i + 20); } vector<int> vtar; vtar.resize(v.size()); transform(v.begin(), v.end(), v2.begin(), vtar.begin(), TransForm2()); for_each(vtar.begin(), vtar.end(), [](int v) { cout << v << endl; }); }