C++ STL(十五):常用拷贝算法(copy)和替换算法(replace、replace_if、swap)


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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值