C++常用拷贝和替换算法-swap()

C++常用拷贝和替换算法-swap()

功能描述:

  • 互换两个容器的元素
#include 
#include 
#include 
using namespace std;
class myPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
void test01()
{
	vector<int> v1;
	vector<int> v2;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i + 100);
	}
	cout << "交换前: " << endl;
	for_each(v1.begin(), v1.end(), myPrint());
	cout << endl;
	for_each(v2.begin(), v2.end(), myPrint());
	cout << endl;

	cout << "交换后: " << endl;
	swap(v1, v2);
	for_each(v1.begin(), v1.end(), myPrint());
	cout << endl;
	for_each(v2.begin(), v2.end(), myPrint());
	cout << endl;
}
int main()
{
	test01();
	return 0;
}
//swap交换容器时,注意交换的容器要同种类型

C++常用拷贝和替换算法-swap()

你可能感兴趣的:(C++学习笔记,c++)