5.4 常用拷贝和替换算法
算法简介:
copy
//容器内指定元素拷贝到另一容器中replace
//将容器内指定范围的就元素修改为新元素replace_if
//容器内指定范围满足条件的元素替换为新元素swap
//互换两个容器的元素
5.4.1 copy
功能描述:
- 容器内指定元素拷贝到另一容器中
函数原型:
copy(iterator beg, iterator end, iterator dest);
- beg 开始迭代器
- end 结束迭代器
- dest 目标迭代器
示例:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//容器内指定范围的元素拷贝到另一容器中
//打印
class MyPrint {
public:
void operator()(int val) {
cout << val << " ";
}
};
void test01() {
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
vector<int> v2;
v2.resize(v1.size());
copy(v1.begin(), v1.end(), v2.begin());
for_each(v2.begin(), v2.end(), MyPrint());
//深拷贝
v1.clear();
for_each(v2.begin(), v2.end(), MyPrint());
}
//主函数
int main() {
test01();
system("pause");
return 0;
}
总结:利用copy算法在拷贝时,目标容器记得提前开辟空间
5.4.2 replace
功能描述:
- 将容器内只当范围的就元素修改为新元素
函数原型:
replace(iterator beg, iterator end, oldvalue, newvalue);
- 将区间内旧元素 替换成 新元素
- beg 开始迭代器
- end 结束迭代器
- oldvalue 旧元素
- newvalue 新元素
示例:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//将容器内只当范围的就元素修改为新元素 replace
//打印输出
void myPrint(int val) {
cout << val << " ";
}
void test01() {
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(10);
v.push_back(30);
v.push_back(10);
for_each(v.begin(), v.end(), myPrint);
cout << endl;
//注意:将所有的指定值都替换
replace(v.begin(), v.end(), 10, 50);
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
//主函数
int main() {
test01();
system("pause");
return 0;
}
总结:replace 会替换区间内满足条件的元素
5.4.3 replace_if
功能描述:
- 将区间内满足条件的元素,替换成指定元素
函数原型:
replace_if(iterator beg, iterator end, _Pred, newvalue);
- 按条件替换元素,满足条件的替换成指定元素
- beg 开始迭代器
- end 结束迭代器
- _Pred 谓词
- newvalue 新元素
示例:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//将区间内满足条件的元素,替换成指定元素 replace_if
//谓词(小于6)
class LessSix {
public:
bool operator()(int val) {
return val < 6;
}
};
//打印
void myPrint(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(), myPrint);
cout << endl;
//将小于6的数全部替换为100
replace_if(v.begin(), v.end(), LessSix(), 100);
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
//主函数
int main() {
test01();
system("pause");
return 0;
}
5.4.4 swap
功能描述:
- 互换两个容器的元素
函数原型:
swap(container c1, container c2);
- 互换两个容器
- c1 容器1
- c2 容器2
示例:
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
//互换两个容器的元素 swap
//打印
class myPrint {
public:
void operator()(int val) {
cout << val << " ";
}
};
void test01() {
vector<int> v1;
for (int i = 0; i < 10; ++i) {
v1.push_back(i);
}
cout << "v1:";
for_each(v1.begin(), v1.end(), myPrint());
cout << endl;
vector<int> v2;
for (int i = 5; i > 0; --i) {
v2.push_back(i);
}
cout << "v2:";
for_each(v2.begin(), v2.end(), myPrint());
cout << endl;
//互换容器
swap(v1, v2);
cout << "互换容器后:" << endl;
cout << "v1:";
for_each(v1.begin(), v1.end(), myPrint());
cout << endl;
cout << "v2:";
for_each(v2.begin(), v2.end(), myPrint());
cout << endl;
}
//主函数、
int main() {
test01();
system("pause");
return 0;
}
总结:同种容器类型才可交换