swap [C++ Reference]
Keywords: stl,list,swap
Date: 2009-10-09T04:47:39-0700

## swap

Syntax:

        #include <list>
        void swap( list& from );

The swap() function exchanges the elements of the current list 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 lists:

        list<string> l1;
        l1.push_back("I'm in l1!");

        list<string> l2;
        l2.push_back("And I'm in l2!");

        l1.swap(l2);

        cout << "The first element in l1 is " << l1.front() << endl;
        cout << "The first element in l2 is " << l2.front() << endl;

The above code displays:

        The first element in l1 is And I'm in l2!
        The first element in l2 is I'm in l1!

Related Topics: splice