unordered_multimap swap() function in C++ STL

The unordered_multimap::swap() is a built-in function in C++ STL which swaps the contents of two unordered_multimap containers. The sizes can differ of both the containers.

Syntax:

unordered_multimap_name1.swap(unordered_multimap_name2)

Parameters: The function accepts a single mandatory parameter unordered_multimap_name2 which specifies the unordered_multimap with which the swapping of unordered_multimap_name1 is to be done.



Return Value: It does not return anything.

Below programs illustrates the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multimap::swap()
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<int, int> sample1, sample2;
  
    // inserts key and element
    // in sample1
    sample1.insert({ 10, 100 });
    sample1.insert({ 50, 500 });
  
    // inserts key and element
    // in sample1
    sample2.insert({ 20, 200 });
    sample2.insert({ 30, 300 });
    sample2.insert({ 30, 150 });
  
    cout << "Key and Elements of Sample1 before swap are:";
    for (auto it = sample1.begin(); it != sample1.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    cout << "\nKey and Elements of Sample2 before swap are:";
    for (auto it = sample2.begin(); it != sample2.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    // performs swap of two
    // unordered_multimaps
    sample1.swap(sample2);
  
    cout << "\n\nKey and Elements of Sample1 after swap are:";
    for (auto it = sample1.begin(); it != sample1.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    cout << "\nKey and Elements of Sample2 after swap are:";
    for (auto it = sample2.begin(); it != sample2.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    return 0;
}

chevron_right


Output:

Key and Elements of Sample1 before swap are:{50, 500} {10, 100} 
Key and Elements of Sample2 before swap are:{20, 200} {30, 150} {30, 300} 

Key and Elements of Sample1 after swap are:{20, 200} {30, 150} {30, 300} 
Key and Elements of Sample2 after swap are:{50, 500} {10, 100}

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multimap::swap()
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<char, char> sample1, sample2;
  
    // inserts key and element
    // in sample1
    sample1.insert({ 'a', 'A' });
    sample1.insert({ 'g', 'G' });
  
    // inserts key and element
    // in sample1
    sample2.insert({ 'b', 'B' });
    sample2.insert({ 'c', 'C' });
    sample2.insert({ 'd', 'D' });
  
    cout << "Key and Elements of Sample1 before swap are:";
    for (auto it = sample1.begin(); it != sample1.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    cout << "\nKey and Elements of Sample2 before swap are:";
    for (auto it = sample2.begin(); it != sample2.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    // performs swap of two
    // unordered_multimaps
    sample1.swap(sample2);
  
    cout << "\n\nKey and Elements of Sample1 after swap are:";
    for (auto it = sample1.begin(); it != sample1.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    cout << "\nKey and Elements of Sample2 after swap are:";
    for (auto it = sample2.begin(); it != sample2.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    return 0;
}

chevron_right


Output:

Key and Elements of Sample1 before swap are:{g, G} {a, A} 
Key and Elements of Sample2 before swap are:{d, D} {b, B} {c, C} 

Key and Elements of Sample1 after swap are:{d, D} {b, B} {c, C} 
Key and Elements of Sample2 after swap are:{g, G} {a, A}


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.