读书人

怎么快速交换两个向量容器中的内容

发布时间: 2013-10-08 17:02:59 作者: rapoo

如何快速交换两个向量容器中的内容

#include "stdafx.h"

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

voidPrintInt(constint&nData)

{

cout<<nData<<endl;

}

int_tmain(int argc, _TCHAR* argv[])

{

vector<int> vecInt1,vecInt2;

for(int i=0; i<5;++i)

{

vecInt1.push_back(i);

}

cout<<"向量中的内容为:"<<endl;

for_each(vecInt1.begin(),vecInt1.end(),PrintInt);

cout<<"向量中的内容为:"<<endl;

for_each(vecInt2.begin(),vecInt2.end(),PrintInt);

vecInt2.swap(vecInt1);//交互向量

cout<<"向量中的内容为:"<<endl;

for_each(vecInt1.begin(),vecInt1.end(),PrintInt);

cout<<"向量中的内容为:"<<endl;

for_each(vecInt2.begin(),vecInt2.end(),PrintInt);

return 0;

}

执行结果:

怎么快速交换两个向量容器中的内容

读书人网 >编程

热点推荐