C++ Unordered_map::swap() Function



The C++ std::unordered_map::swap() function is used to exchange the content of the first unordered_map with another unordered_map. The swapping occurs when both unordered_maps elements of the same type.

This function does not depend on size. If both the unordered_map have the same type, it swaps the elements of the underored_maps perfectly, whether the size is the same or different.

Syntax

Following is the syntax of non member std::unordered_map::swap() function.

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

Parameters

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

Return value

This function does not returns any things.

Example 1

In the following example, we are going to perform swap between the two containers one is with elements and another one is empty.

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

Output

Let us compile and run the above program, this will produce the following result −

Unordered map contains following elements
e = 5
a = 1
b = 2
c = 3
d = 4

Example 2

Consider the another scenario, where we are going to make both the unordered_map swap each other.

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

Output

Following is the output of the above code −

um1 contains following elements before swapping: 
e = 5
d = 4
c = 3
b = 2
a = 1
um2 contains following elements before swapping: 
D = 4
C = 3
B = 2
A = 1
um1 contains following elements after swapping: 
D = 4
C = 3
B = 2
A = 1
um2 contains following elements after swapping: 
e = 5
d = 4
c = 3
b = 2
a = 1

Example 3

Let's look at the following example, where we are going to create two unordered maps, John and Bob that store the subject's marks, and now we are swapping the marks of Jhon and Bob against each other.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_map<string, int> John = {
      {"CHE", 85},
      {"PHY", 88},
      {"MAT", 99},
      {"CMS", 86},
      {"ENG", 95}
   };
   unordered_map<string, int> Bob = {
      {"PHY", 90},
      {"CHE", 82},
      {"MAT", 98},
      {"CMS", 88}
   };
   cout<<"Swapping the marks of first unordered map with second unordered_map"<<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 map with second unordered_map
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