stl:vector:swap

C++ Reference

swap

Syntax:

    #include <vector>
    void swap( container& from );

The swap() function exchanges the elements of the current vector with those of from. This function operates in constant time.

For example, the following code uses the swap() function to exchange the contents of two vectors:

    vector<string> v1;
    v1.push_back("I'm in v1!");
 
    vector<string> v2;
    v2.push_back("And I'm in v2!");
 
    v1.swap(v2);
 
    cout << "The first element in v1 is " << v1.front() << endl;
    cout << "The first element in v2 is " << v2.front() << endl;

The above code displays:

    The first element in v1 is And I'm in v2!
    The first element in v2 is I'm in v1!

Related Topics: = operator