Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
map
comparison operators
map::map
map::~map
member functions:
map::begin
map::clear
map::count
map::empty
map::end
map::equal_range
map::erase
map::find
map::get_allocator
map::insert
map::key_comp
map::lower_bound
map::max_size
map::operator=
map::operator[]
map::rbegin
map::rend
map::size
map::swap
map::upper_bound
map::value_comp


map::swap

public member function
void swap ( map<Key,T,Compare,Allocator>& mp );

Swap content

Exchanges the content of the container with the content of mp, which is another map 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 mp before the call, and the elements of mp 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

mp
Another map container of the same type as this whose content is swapped with that of this container.

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// swap maps
#include <iostream>
#include <map>
using namespace std;
int main ()
{
  map<char,int> foo;
  map<char,int> bar;
  map<char,int>::iterator it;
  foo['x']=100;
  foo['y']=200;
  bar['a']=11;
  bar['b']=22;
  bar['c']=33;
  foo.swap(bar);
  cout << "foo contains:\n";
  for ( it=foo.begin() ; it != foo.end(); it++ )
    cout << (*it).first << " => " << (*it).second << endl;
  cout << "bar contains:\n";
  for ( it=bar.begin() ; it != bar.end(); it++ )
    cout << (*it).first << " => " << (*it).second << endl;
  return 0;
}


Output:
foo contains:
a => 11
b => 22
c => 33
bar contains:
x => 100
y => 200

Complexity

Constant.

See also