功能描述:
互换两个容器的元素
函数原型:
swap(containr c1, container c2);
//互换两个容器的元素
//c1容器1
//c2容器2
代码示例:
#include
#include
#include
using namespace std;
void myprint(int val)
{
cout << val << " ";
}
void test01()
{
vectorv1;
vectorv2;
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;
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();
system("pause");
return 0;
}
总结:swap交换容器时,注意交换的容器要同种类型