c++ 交换两个容器(swap)

 

#include <iostream>
#include <vector>
using namespace std;
int main ()
{
    vector<int> foo (3,100);   // three ints with a value of 100
    vector<int> bar (5,200);   // five ints with a value of 200
    
    foo.swap(bar);
    
    cout << "foo contains:";
    for (unsigned i=0; i<foo.size(); i++)
        cout << ' ' << foo[i];
    cout << '\n';
    
    cout << "bar contains:";
    for (unsigned i=0; i<bar.size(); i++)
        cout << ' ' << bar[i];
    cout << '\n';
    
    return 0;
}

 

posted @ 2018-12-12 13:06  anobscureretreat  阅读(322)  评论(0编辑  收藏  举报