Friday, January 1, 2010

STL LIST- SWAP()

list::swap

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.

// swap lists
#include
#include
using namespace std;

main ()
{
list first (3,100);
// three ints with a value of 100
list second (5,200);
// five ints with a value of 200
list::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
second contains: 100 100 100

No comments:

Post a Comment