swap

Syntax:

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

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

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

    deque<int> v( 5, 1 );
    deque<int> d( 4, 42 );
    v.swap( d );
 
    size_t i;
    cout << "v contains: ";
    for( i = 0; i < v.size(); ++i ) cout << v[i] << ' ';
    cout << '\n';
 
    cout << "d contains: ";
    for( i = 0; i < d.size(); ++i ) cout << d[i] << ' ';
    cout << '\n';

The above code displays:

    v contains: 42 42 42 42
    d contains: 1 1 1 1 1

Related Topics: insert, оператор =