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 << '
';
    
    cout << "bar contains:";
    for (unsigned i=0; i<bar.size(); i++)
        cout << ' ' << bar[i];
    cout << '
';
    
    return 0;
}

原文地址:https://www.cnblogs.com/sea-stream/p/10107744.html