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. The swap will happen when the other container is of the same type.

This function exchanges the element or key/value pairs without actually performing any copies or moves on the individual element, allowing for constant-time execution no matter the size.

Syntax

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

void swap(unordered_map& ump);

Parameters

  • ump − it indicates the container to swap with.

Return value

This function does not returns any value.

Example 1

In the following example, let's see the usage of swap() function.

#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;
   um1.swap(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

In the following example, we are going to perform the swapping between to containers and exchanging their value in viceversa.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_map<char, int> um1 = { {'a', 1}, {'b', 2}, {'c', 3}, {'d', 4} };
   unordered_map<char, int> um2 = { {'e', 5}, {'f', 6}, {'g', 7}, {'h', 8} };
   cout<<"um1 contains following element before swap: "<<endl;
   for(auto & it:um1){
      cout<<it.first<< " = "<<it.second<<endl;
   }
   cout<<"um2 contains following element before swap: "<<endl;
   for(auto & it:um2){
      cout<<it.first<< " = "<<it.second<<endl;
   }
   um1.swap(um2);
   cout << "um1 contains following elements after swap: " << endl;
   for (auto it = um1.begin(); it != um1.end(); ++it)
      cout << it->first << " = " << it->second << endl;
      cout << "um2 contains following elements after swap: " << 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 element before swap: 
d = 4
c = 3
b = 2
a = 1
um2 contains following element before swap: 
h = 8
g = 7
f = 6
e = 5
um1 contains following elements after swap: 
h = 8
g = 7
f = 6
e = 5
um2 contains following elements after swap: 
d = 4
c = 3
b = 2
a = 1

Example 3

Consider the following example, where we are going to create a two maps one is with using [] operator, another one normally and exchanging their values.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_map<string, int> um1;
   um1["Vivek"] = 80;
   um1["Aman"] = 85;
   um1["Akash"] = 90;
   unordered_map<string, int> um2 = { {"Sarika", 65}, {"Revathi", 95}, {"Daniel", 100} };
   cout<<"um1 contains following element before swap: "<<endl;
   for(auto & it:um1){
      cout<<it.first<< " = "<<it.second<<endl;
   }
   cout<<"um2 contains following element before swap: "<<endl;
   for(auto & it:um2){
      cout<<it.first<< " = "<<it.second<<endl;
   }
   um1.swap(um2);
   cout << "um1 contains following elements after swap: " << endl;
   for (auto it = um1.begin(); it != um1.end(); ++it)
      cout << it->first << " = " << it->second << endl;
      cout << "um2 contains following elements after swap: " << endl;
   for (auto it = um2.begin(); it != um2.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}

Output

Output of the above code is as follows −

um1 contains following element before swap: 
Akash = 90
Aman = 85
Vivek = 80
um2 contains following element before swap: 
Revathi = 95
Daniel = 100
Sarika = 65
um1 contains following elements after swap: 
Revathi = 95
Daniel = 100
Sarika = 65
um2 contains following elements after swap: 
Akash = 90
Aman = 85
Vivek = 80
Advertisements