swap [C++ Reference]
Keywords: stl,vector,swap
Date: 2009-10-09T04:32:37-0700

[[swap]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- cz
- de
- es
- fr
- it
- jp
- nl
- pl
- ro
- ru
- sk
- tr
- tw

## swap

Syntax:

        #include <vector>
        void swap( vector& 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

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •