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

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

#include "stdafx.h"

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

 

voidPrintInt(const int&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;

}

执行结果:

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