list::swap | public member function |
void swap ( list<T,Allocator>& lst ); |
Swap content
Exchanges the content of the vector by the content of lst, which is another list object containing elements of the same type. Sizes may differ.
After the call to this member function, the elements in this container are those which were in lst before the call, and the elements of lst are those which were in this. All iterators, references and pointers remain valid for the swapped objects.
Notice that a global algorithm function exists with this same name, swap, and the same behavior.
Parameters
- lst
- Another list container of the same type as this whose content is swapped with that of this container.
Return value
noneExample
// swap lists #include <iostream> #include <list> using namespace std; main () { list<int> first (3,100); // three ints with a value of 100 list<int> second (5,200); // five ints with a value of 200 list<int>::iterator it; first.swap(second); cout << "first contains:"; for (it=first.begin(); it!=first.end(); it++) cout << " " << *it; cout << "\nsecond contains:"; for (it=second.begin(); it!=second.end(); it++) cout << " " << *it; cout << endl; return 0; } |
Output:
first contains: 200 200 200 200 200 |
Complexity
Constant.See also
swap