Open In App

unordered_map swap in C++ STL

Last Updated : 14 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
The std::unordered_map::swap() is a built in function in C++ STL which swaps the elements of a container to an other container. After the call of this function elements of the caller unordered_map will be elements of called unordered_map while elements of called unordered_map will be elements of caller unordered_map. Internally swapping of elements is not done only reference type of both unordered_map is changed. Syntax
unordered_map.swap ( unordered_map& ump )
Return type : Return type of this function is void. Parameters : An other unordered_map with same type of elements. Complexity : Its complexity is constant. Example 1 CPP
// C++ code to illustrate the method
// unordered_map swap
#include <bits/stdc++.h>

using namespace std;

int main()
{
    unordered_map<int, int> sample1, sample2;

    // Map initialization
    sample1 = { { 2, 2 }, { 3, 4 }, { 4, 6 }, { 5, 8 } };
    sample2 = { { 10, 11 }, { 12, 13 }, { 14, 15 }, { 26, 17 } };

    // printing details before calling swap

    cout << " Elements of maps before swap \n";
    cout << " Elements of first map are : \n";
    for (auto& x : sample1)
        cout << x.first << " : " << x.second << endl;

    cout << " Elements of second map are : \n";
    for (auto& x : sample2)
        cout << x.first << " : " << x.second << endl;

    // swapping
    sample1.swap(sample2);

    cout << " Elements of maps after swap \n";
    cout << " Elements of first map are : \n";
    for (auto& x : sample1)
        cout << x.first << " : " << x.second << endl;

    cout << " Elements of second map are : \n";
    for (auto& x : sample2)
        cout << x.first << " : " << x.second << endl;
    return 0;
}
Output:
Elements of maps before swap 
 Elements of first map are : 
5 : 8
4 : 6
3 : 4
2 : 2
 Elements of second map are : 
14 : 15
26 : 17
12 : 13
10 : 11
 Elements of maps after swap 
 Elements of first map are : 
14 : 15
26 : 17
12 : 13
10 : 11
 Elements of second map are : 
5 : 8
4 : 6
3 : 4
2 : 2
Example 2 CPP
// C++ code to illustrate the method
// unordered_map swap
#include <bits/stdc++.h>

using namespace std;

int main()
{
    unordered_map<char, int> sample1, sample2;

    // Map initialization
    sample1 = { { 'a', 2 }, { 'b', 4 }, { 'c', 6 }, { 'd', 8 } };
    sample2 = { { 'e', 11 }, { 'f', 13 }, { 'h', 15 } };

    // printing details before calling swap

    cout << " Elements of maps before swap \n";
    cout << " Elements of first map are : \n";
    for (auto& x : sample1)
        cout << x.first << " : " << x.second << endl;

    cout << " Elements of second map are : \n";
    for (auto& x : sample2)
        cout << x.first << " : " << x.second << endl;

    // swapping
    sample1.swap(sample2);

    cout << " Elements of maps after swap \n";
    cout << " Elements of first map are : \n";
    for (auto& x : sample1)
        cout << x.first << " : " << x.second << endl;

    cout << " Elements of second map are : \n";
    for (auto& x : sample2)
        cout << x.first << " : " << x.second << endl;
    return 0;
}
Output:
Elements of maps before swap 
 Elements of first map are : 
d : 8
c : 6
b : 4
a : 2
 Elements of second map are : 
h : 15
f : 13
e : 11
 Elements of maps after swap 
 Elements of first map are : 
h : 15
f : 13
e : 11
 Elements of second map are : 
d : 8
c : 6
b : 4
a : 2
Note : Caller and called unordered_map both should contain same type of elements otherwise we will get compile time error.

Similar Reads