set::swap | public member function |
void swap ( set<Key,Compare,Allocator>& st ); |
Swap content
Exchanges the content of the container with the content of st, which is another set 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 st before the call, and the elements of st 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
- st
- Another set container of the same type as this whose content is swapped with that of this container.
Return value
noneExample
// swap sets #include <iostream> #include <set> using namespace std; main () { int myints[]={12,75,10,32,20,25}; set<int> first (myints,myints+3); // 10,12,75 set<int> second (myints+3,myints+6); // 20,25,32 set<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: 20 25 32 |
Complexity
Constant.See also
swap