文章目录
0 常用拷贝算法【copy】和替换算法【replace、replace_if、swap】简介
算法简介:
copy
:拷贝容器元素至另一容器。
replace
:将容器内的指定值全部替换为新值。
replace_if
:按条件将容器内指定范围的旧元素替换为新元素。
swap
:互换两个相同类型容器的元素。
1 copy【拷贝容器元素至另一容器】
作用:拷贝容器元素至另一容器。
注1:使用
copy
算法时,需包含头文件include <algorithm>
。
注2:使用copy
算法时,需为目标容器提前开辟内存空间,如dest.resize(src.size());
,否则程序运行时崩溃。
函数原型:
copy(iterator begin, iterator end, iterator dest);
参数解释:
begin
:源容器迭代器的起始位置;
end
:源容器迭代器的结束位置;
dest
:目标容器迭代器的起始位置。
注:实际开发时,
copy
算法应用较少,建议使用容器的赋值操作。
示例:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm> //使用copy算法
int main() {
vector<int> src;
for (int i = 0; i < 5; i++) {
src.push_back(i);
}
for_each(src.begin(), src.end(), [](int val) {cout << val << " "; }); //0 1 2 3 4
//copy算法拷贝元素时,需为目标容器提前开辟内存空间
vector<int> dest;
dest.resize(src.size());
//copy
copy(src.begin(), src.end(), dest.begin());
for_each(dest.begin(), dest.end(), [](int val) {cout << val << " "; }); //0 1 2 3 4
return 0;
}
2 replace【将指定值全部替换为新值】
作用:将容器内的指定值的元素全部替换为新值。
注:使用
replace
算法时,需包含头文件include <algorithm>
。
函数原型:
replace(iterator begin, iterator end, oldvalue, newvalue);
参数解释:
begin
:迭代器起始位置;
end
:迭代器结束位置;
oldvalue
:被替换的旧元素;
oldvalue
:替换的新元素。
示例:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm> //使用replace算法
int main() {
vector<int> v;
v.push_back(9);
v.push_back(7);
v.push_back(1);
v.push_back(7);
v.push_back(6);
v.push_back(7);
v.push_back(3);
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; }); //9 7 1 7 6 7 3
//replace():将容器内的指定值全部替换为新值
replace(v.begin(), v.end(), 7, 0);
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; }); //9 0 1 0 6 0 3
return 0;
}
3 replace_if【将满足指定条件的元素全部替换为新元素】
作用:将容器内满足指定条件的元素全部替换为新元素。
注:使用
replace_if
算法时,需包含头文件include <algorithm>
。
函数原型:
replace_if(iterator begin, iterator end, _pred, newvalue);
参数解释:
begin
:迭代器起始位置;
end
:迭代器结束位置;
_pred
:指定条件。
①普通回调函数;
②谓词(返回类型为bool
的仿函数);
③匿名函数(lambda表达式)。
newvalue
:替换的新元素。
示例:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm> //使用replace_if算法
//回调函数
bool lessThanFive(int val) {
return val < 5;
}
//谓词(返回类型为bool的仿函数/函数对象)
class LessThanFive {
public:
bool operator()(int val) {
return val < 5;
}
};
//将小于5的元素全部替换为100
int main() {
vector<int> v;
v.push_back(9);
v.push_back(1);
v.push_back(7);
v.push_back(6);
v.push_back(3);
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; }); //9 1 7 6 3
//回调函数
//replace_if(v.begin(), v.end(), lessThanFive, 100);
//for_each(v.begin(), v.end(), [](int val) {cout << val << " "; }); //9 100 7 6 100
//谓词
//replace_if(v.begin(), v.end(), LessThanFive(), 100);
//for_each(v.begin(), v.end(), [](int val) {cout << val << " "; }); //9 100 7 6 100
//匿名函数(lambda表达式)
replace_if(v.begin(), v.end(), [](int val) {return val < 5; }, 100);
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; }); //9 100 7 6 100
return 0;
}
4 swap【互换两个同类型容器的元素】
作用:互换两个相同类型容器的元素。
注:使用
swap
算法时,需包含头文件include <algorithm>
。
函数原型:
swap(container c1, container c2);
参数解释:
c1
:容器1;
c2
:容器2。
示例:
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm> //使用swap算法
int main() {
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 5; i++) {
v1.push_back(i);
v2.push_back(-i);
}
cout << "交换前..." << endl;
for_each(v1.begin(), v1.end(), [](int val) {cout << val << " "; }); //0 1 2 3 4
cout << endl;
for_each(v2.begin(), v2.end(), [](int val) {cout << val << " "; }); //0 -1 -2 -3 -4
cout << endl;
//交换
swap(v1, v2);
cout << "交换后..." << endl;
for_each(v1.begin(), v1.end(), [](int val) {cout << val << " "; }); //0 -1 -2 -3 -4
cout << endl;
for_each(v2.begin(), v2.end(), [](int val) {cout << val << " "; }); //0 1 2 3 4
cout << endl;
return 0;
}