首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2013-10-08 
如何快速交换两个向量容器中的内容#include stdafx.h#include iostream#include vector#include al

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

#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;

}

执行结果:

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

热点排行