Multimap Swap Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Multimap Swap Function

Multimap Swap Function

BLUF: Mastering Multimap Swap Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Multimap Swap Function

C++ is renowned for its efficiency. Learn how Multimap Swap Function enables low-level control and high-performance computing in the tutorial below.

The swap function in C++ for multimap is employed to exchange the contents of two multimaps, requiring both multimaps to be of the same type, even if their sizes differ.

Syntax

Example

void swap (multimap& x);

Parameter

x: multimap container for swapping the data.

Return value

Complexity

Constant.

Iterator validity

All iterators, references, and pointers pointing to elements within either container will remain valid; however, they will now reference elements within the alternate container and iterate accordingly.

Data races

Both the container and x are modified.

Exception safety

No effect on container if exception is thrown.

Example 1

Let's examine a straightforward example demonstrating how to exchange elements between two multimaps:

Example

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   multimap<char, int> m1 = {
      {'a', 1},
      {'b', 2},
      {'c', 3},
      {'b', 4},
      {'c', 5},
      };

   multimap<char, int> m2;

   m2.swap(m1);

   cout << "Multimap m2 contains following elements" << endl;

   for (auto it = m2.begin(); it != m2.end(); ++it)
      cout << it->first << " = " << it->second << endl;

   return 0;
}

Output:

Output

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

In the prior scenario, the multimap m1 contains five elements while m2 remains devoid of any elements. Upon swapping m1 with m2, all the elements originally present in m1 are transferred to m2.

Example 2

Let's examine a straightforward example demonstrating how to swap the contents of two multimaps:

Example

#include <iostream>
#include <map>

using namespace std;

int main ()
{
  multimap<char,int> multimap1,multimap2;

  multimap1 = {
              {'x', 100},
              {'x', 200}
              };

  multimap2 = {
              {'a', 110},
              {'b', 220},
              {'a', 330}
              };

  multimap1.swap(multimap2);

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

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

  return 0;
}

Output:

Output

multimap1 contains:
a => 110
a => 330
b => 220
multimap2 contains:
x => 100
x => 200

In the given scenario, the contents of two multimaps, specifically multimap1 and multimap2, are swapped with each other.

Example 3

Let's examine a basic illustration of exchanging the contents of two multimaps:

Example

#include<iostream>
#include<map>

using namespace std;
 
int main()
{
    // Take any two multimaps
    multimap<int, char> multimap1, multimap2;
 
    multimap1 = {
                {1, 'a'},
                {2, 'b'},
                {2, 'c'},
                {4, 'd'} };
 
    multimap2 = {
                {1, 'x'},
                {2, 'y'},
                {2, 'z'} };
 
    // Swap elements of multimaps
    swap(multimap1, multimap2);
 
    // Print the elements of multimaps
    cout << "multimap1:\n"<< "\tKEY\tELEMENT\n";
    for (auto it = multimap1.begin();
         it != multimap1.end(); it++)
 
        cout << "\t" << it->first << "\t" << it->second << '\n';
 
    cout << "multimap2:\n"<< "\tKEY\tELEMENT\n";
    for (auto it = multimap2.begin();
         it != multimap2.end(); it++)
 
        cout << "\t" << it->first << "\t" << it->second << '\n';
 
    return 0;
}

Output:

Output

multimap1:
	KEY	ELEMENT
	1	x
	2	y
	2	z
multimap2:
	KEY	ELEMENT
	1	a
	2	b
	2	c
	4	d

In the example above, an alternative version of the swap function is employed to exchange the contents of two multimaps.

Example 4

Let's see a simple example:

Example

#include <iostream>
#include <string>
#include <map>

using namespace std;

void show(const char *msg, multimap<string, int> mp);

int main() {
  multimap<string, int> m1, m2;

  m1.insert(pair<string, int>("A", 100));
  m1.insert(pair<string, int>("B", 300));
  m1.insert(pair<string, int>("B", 200));

  // Exchange the contents of m1 and m2.
  cout << "Exchange m1 and m2.\n";
  m1.swap(m2);
  show("Contents of m2: ", m2);
  show("Contents of m1: ", m1);

 // Clear m1.
  m1.clear();
  if(m1.empty()) cout << "m1 is now empty.";

  return 0;
}

// Display the contents of a multimap<string, int> by using an iterator.
void show(const char *msg, multimap<string, int> mp) {
  multimap<string, int>::iterator itr;

  cout << msg << endl;
  for(itr=mp.begin(); itr != mp.end(); ++itr)
    cout << "  " << itr->first << ", " << itr->second << endl;
  cout << endl;
}

Output:

Output

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

Contents of m1: 

m1 is now empty.

After transferring the contents from multimap m1 to multimap m2, the multimap m1 has been emptied.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience