8.1 copy 复制 目标容器有容量
8.2 replace 替换 按值替换
8.3 replace _if 按条件替换
8.4 swap 交换
copy
#include<iterator>
void test01() {
vector<int>v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
vector<int>vTarget;
vTarget.resize(v.size());
copy(v.begin(), v.end(), vTarget.begin());
for_each(vTarget.begin(), vTarget.end(), [](int val) {cout << val << " "; });
cout << endl;
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/dd132683e0fb66101b1b1545d742d74a.png)
replace
void test02() {
vector<int>v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
replace(v.begin(), v.end(), 3, 300);
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8f7f3534ced4f8e1221442e053dc0667.png)
replace_if
void test03() {
vector<int>v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
replace_if(v.begin(), v.end(), MyCompare(), 3000);
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1302d499c30bd5a9340240772bdd1d03.png)
swap
void test04() {
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; ++i) {
v1.push_back(i * 9);
v2.push_back(i * 5);
}
cout << "交换前数据:" << endl;
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
swap(v1, v2);
cout << "交换后数据:" << endl;
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/29ddde7838b44bd22eb90fe856da335b.png)