cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : set : swap
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
set
comparison operators
set::set
set::~set
member functions:
· set::begin
· set::clear
· set::count
· set::empty
· set::end
· set::equal_range
· set::erase
· set::find
· set::get_allocator
· set::insert
· set::key_comp
· set::lower_bound
· set::max_size
· set::operator=
· set::rbegin
· set::rend
· set::size
· set::swap
· set::upper_bound
· set::value_comp

-

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

none

Example

// 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
second contains: 10 12 75

Complexity

Constant.

See also

swap

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us