public member function
<map>

std::multimap::swap

void swap (multimap& x);
Swap content
Exchanges the content of the container by the content of x, which is another multimap of the same type. Sizes may differ.

After the call to this member function, the elements in this container are those which were in x before the call, and the elements of x are those which were in this. All iterators, references and pointers remain valid for the swapped objects.

Notice that a non-member function exists with the same name, swap, overloading that algorithm with an optimization that behaves like this member function.

Whether the internal container allocators and comparison objects are swapped is undefined.
Whether the internal container allocators are swapped is not defined, unless in the case the appropriate allocator trait indicates explicitly that they shall propagate.

The internal comparison objects are always exchanged, using swap.

Parameters

x
Another multimap container of the same type as this (i.e., with the same template parameters, Key, T, Compare and Alloc) 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
// swap multimaps
#include <iostream>
#include <map>

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

  foo.insert(std::make_pair('x',100));
  foo.insert(std::make_pair('y',200));

  bar.insert(std::make_pair('a',11));
  bar.insert(std::make_pair('b',22));
  bar.insert(std::make_pair('a',55));

  foo.swap(bar);

  std::cout << "foo contains:\n";
  for (std::multimap<char,int>::iterator it=foo.begin(); it!=foo.end(); ++it)
    std::cout << (*it).first << " => " << (*it).second << '\n';

  std::cout << "bar contains:\n";
  for (std::multimap<char,int>::iterator it=bar.begin(); it!=bar.end(); ++it)
    std::cout << (*it).first << " => " << (*it).second << '\n';

  return 0;
}


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

Complexity

Constant.

Iterator validity

All iterators, pointers and references referring to elements in both containers remain valid, but now are referring to elements in the other container, and iterate in it.
Note that the end iterators do not refer to elements and may be invalidated.

Data races

Both the container and x are modified.
No contained elements are accessed by the call (although see iterator validity above).

Exception safety

If the allocators in both containers compare equal, or if their allocator traits indicate that the allocators shall propagate, the function never throws exceptions (no-throw guarantee).
Otherwise, it causes undefined behavior.

See also

  • 997351548 2018-02-20
  • 903691547 2018-02-20
  • 9127171546 2018-02-20
  • 2234751545 2018-02-20
  • 1685381544 2018-02-20
  • 911091543 2018-02-20
  • 9281021542 2018-02-20
  • 632231541 2018-02-20
  • 9751491540 2018-02-20
  • 8333621539 2018-02-20
  • 995001538 2018-02-20
  • 6477441537 2018-02-20
  • 511981536 2018-02-19
  • 2072881535 2018-02-19
  • 6024461534 2018-02-19
  • 7594061533 2018-02-19
  • 9311341532 2018-02-19
  • 4928331531 2018-02-19
  • 38561530 2018-02-19
  • 7776611529 2018-02-19