Javatpoint Logo
Javatpoint Logo

C++ multimap swap(multimap)

C++ swap(multimap) is a non-member function of multimap in C++. This is used to swap (or exchange) the contents of two multimaps (i.e. x and y) but both the multimaps must be of same type although sizes may differ.

Syntax

Parameter

x: First multimap object.

y: Second multimap object of the same type.

Return value

None

Complexity

Constant.

Iterator validity

All iterators, references and pointers referring to elements in both containers remain valid.

Note that the end iterators do not refer to elements and may be invalidated.

Data Races

Both containers x and y are modified.

No contained elements are accessed by the call.

Exception Safety

This function does not throw an exception.


Example 1

Let's see the simple example to swap the element of one multimap to another:

Output:

Multimap contains following elements
a = 1
b = 2
b = 3
c = 4
e = 5

In the above example, multimap m1 has five elements and m2 is empty. When you swap m1 to m2 then all the elements of m1 is swapped to m2.

Example 2

Let's see a simple example to exchange the contents of two multimaps:

Output:

multimap1 contains:
a => 110
c => 220
c => 330

multimap2 contains:
x => 100
y => 200

In the above example, contents of two multimaps i.e. multimap1 and multimap2 are exchanged to each other.

Example 3

Let's see a simple example to swap the contents of two multimaps:

Output:

m1 : {[5,d], [15,e], }
m2 : {[10,a], [10,c], [20,b], }

Example 4

Let's see a simple example:

Output:

Exchange m1 and m2.
Contents of m2: 
  A, 100
  B, 200
  G, 300

Contents of m1: 
m1 is now empty.

In the above example, contents of multimap m1 are swapped to multimap m2 and after swapping m1 multimap have been cleared.


Next TopicC++ Multimap




Help Others, Please Share

facebook twitter pinterest