cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : multimap : 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
multimap
comparison operators
multimap::multimap
multimap::~multimap
member functions:
· multimap::begin
· multimap::clear
· multimap::count
· multimap::empty
· multimap::end
· multimap::equal_range
· multimap::erase
· multimap::find
· multimap::get_allocator
· multimap::insert
· multimap::key_comp
· multimap::lower_bound
· multimap::max_size
· multimap::operator=
· multimap::rbegin
· multimap::rend
· multimap::size
· multimap::swap
· multimap::upper_bound
· multimap::value_comp

-

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

Swap content

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

mmp
Another multimap container of the same type as this whose content is swapped with that of this container.

Return value

none

Example

// swap multimaps
#include <iostream>
#include <map>
using namespace std;

int main ()
{
  multimap<char,int> foo;
  multimap<char,int> bar;
  multimap<char,int>::iterator it;

  foo.insert(pair<char,int>('x',100));
  foo.insert(pair<char,int>('y',200));

  bar.insert(pair<char,int>('a',11));
  bar.insert(pair<char,int>('b',22));
  bar.insert(pair<char,int>('a',55));

  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
a => 55
c => 22
bar contains:
x => 100
y => 200

Complexity

Constant.

See also

swap

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