C++ unordered_multimap::swap() Function



The C++ std::unordered_multimap::swap() function is used to exchange the content of the first unordered_multimap with another unordered_multimap. It will takes place only if both the elements of the unordered_multimaps are of same type. This function does not depend on size.

Syntax

Following is the syntax of std::unordered_multimap::swap() function.

void swap(unordered_multimap<Key,T,Hash,Pred,Alloc>& first, unordered_multimap<Key,T,Hash,Pred,Alloc>& second);

Parameters

  • first − First unordered_multimap object.
  • second − Second unordered_multimap object of same type.

Return value

This function does not returns any things.

Example 1

Let's look at the following example, where we are going to perform the swap between two multimaps using the swap(() function.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<char, int> umm1 = {
      {'a', 1},
      {'b', 2},
      {'c', 3},
      {'b', 4},
      {'c', 5},
   };
   unordered_multimap<char, int> umm2 = {
      {'A', 1},
      {'B', 2},
      {'C', 3},
      {'D', 4}
   };
   cout << "umm1 contains following elements before swapping: " << endl;
   for (auto it = umm1.begin(); it != umm1.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   
   cout << "umm2 contains following elements before swapping: " << endl;
   for (auto it = umm2.begin(); it != umm2.end(); ++it)
      cout << it->first << " = " << it->second << endl;
      
   swap(umm1, umm2);
   
   cout << "umm1 contains following elements after swapping: " << endl;
   for (auto it = umm1.begin(); it != umm1.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   
   cout << "umm2 contains following elements after swapping: " << endl;
   for (auto it = umm2.begin(); it != umm2.end(); ++it)
      cout << it->first << " = " << it->second << endl;
      
   return 0;
}

Output

If we run the above code it will generate the following output −

umm1 contains following elements before swapping: 
c = 5
c = 3
b = 4
b = 2
a = 1
umm2 contains following elements before swapping: 
D = 4
C = 3
B = 2
A = 1
umm1 contains following elements after swapping: 
D = 4
C = 3
B = 2
A = 1
umm2 contains following elements after swapping: 
c = 5
c = 3
b = 4
b = 2
a = 1

Example 2

Consider another scenario where we are going to create two multimaps, one with elements and the other empty, and apply the swap() function to swap elements into the empty multimap.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<char, int> umm1 = {
      {'a', 1},
      {'b', 2},
      {'c', 3},
      {'b', 4},
      {'c', 5},
   };
   unordered_multimap<char, int> umm2;
   swap(umm1, umm2);
   cout << "umm2 unordered multimap contains following elements" << endl;
   for (auto it = umm2.begin(); it != umm2.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}

Output

Following is the output of the above code −

umm2 unordered multimap contains following elements
c = 5
c = 3
b = 4
b = 2
a = 1

Example 3

In the following example, we are going to use the two multimaps and swap the elements of both the multimaps with eachother using the swap() function.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<string, int> John = {
      {"CHE", 85},
      {"PHY", 88},
      {"MAT", 99},
      {"CMS", 86},
      {"ENG", 95}
   };
   unordered_multimap<string, int> Bob = {
      {"PHY", 90},
      {"CHE", 82},
      {"MAT", 98},
      {"CMS", 88}
   };
   cout<<"Swapping the marks of first unordered multimap with second unordered_multimap"<<endl;
   
   swap(John, Bob);
   
   cout <<"John contains following marks after swapping: " << endl;
   for (auto & it: John)
      cout << it.first << " = " << it.second << endl;
   
   cout <<"Bob contains following marks after swapping: " << endl;
   for (auto & it:  Bob)
      cout << it.first << " = " << it.second << endl;
   return 0;
}

Output

Output of the above code is as follows −

Swapping the marks of first unordered multimap with second unordered_multimap
john contains following marks after swapping: 
CMS = 88
MAT = 98
CHE = 82
PHY = 90
Bob contains following marks after swapping: 
ENG = 95
CMS = 86
MAT = 99
PHY = 88
CHE = 85
Advertisements