unordered_multiset swap() in C++ STL

The swap() method of “unordered_multiset” swaps the contents of two containers. It is public member function. This function:

  • Exchanges the content of the container by the content of variable, which is another unordered_multiset object containing elements of the same type but the sizes may differ.
  • After the call to this member function, the elements in this container are those which were in variable before the call, and the elements of variable are those which were in this.

Syntax:

void swap(unordered_multiset &another_unordered_multiset);

Parameters: It receives another_unordered_multiset container object of the same type as this container with which it is to be swapped.

Returns: It does not return any value.

Below program illustrate the unordered_multiset swap() function :-

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
#include <string>
#include <unordered_set>
  
using namespace std;
  
int main()
{
  
    // sets the values in two container
    unordered_multiset<string>
        first = { "FOR GEEKS" },
        second = { "GEEKS" };
  
    // before swap values
    cout << "before swap :- \n";
  
    cout << "1st container : ";
    for (const string& x : first)
        cout << x << endl;
  
    cout << "2nd container : ";
    for (const string& x : second)
        cout << x << endl;
  
    // call swap
  
    first.swap(second);
  
    // after swap values
    cout << "\nafter swap :- \n";
  
    // displaying 1st container
    cout << "1st container : ";
    for (const string& x : first)
        cout << x << endl;
  
    // displaying 2nd container
    cout << "2nd container : ";
    for (const string& x : second)
        cout << x << endl;
  
    return 0;
}

chevron_right


Output:

before swap :- 
1st container : FOR GEEKS
2nd container : GEEKS

after swap :- 
1st container : GEEKS
2nd container : FOR GEEKS

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
#include <string>
#include <unordered_set>
  
using namespace std;
  
int main()
{
  
    // sets the values in two container
    unordered_multiset<int>
        first = { 1, 2, 3 },
        second = { 4, 5, 6 };
  
    // before swap values
    cout << "before swap :- \n";
  
    cout << "1st container : ";
    for (const int& x : first)
        cout << x << " ";
    cout << endl;
  
    cout << "2nd container : ";
    for (const int& x : second)
        cout << x << " ";
    cout << endl;
  
    // call swap
  
    first.swap(second);
  
    // after swap values
    cout << "\nafter swap :- \n";
  
    // displaying 1st container
    cout << "1st container : ";
    for (const int& x : first)
        cout << x << " ";
    cout << endl;
  
    // displaying 2nd container
    cout << "2nd container : ";
    for (const int& x : second)
        cout << x << " ";
    cout << endl;
  
    return 0;
}

chevron_right


Output:

before swap :- 
1st container : 3 2 1 
2nd container : 6 5 4 

after swap :- 
1st container : 6 5 4 
2nd container : 3 2 1 


My Personal Notes arrow_drop_up

2nd year student of Information Technology JADAVPUR UNIVERSITY

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.