A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/unordered_set-swap-in-c-stl/ below:

unordered_set swap() in C++ STL

unordered_set swap() in C++ STL

Last Updated : 09 Oct, 2018

The

swap()

method of

“unordered_set”

swaps the contents of two containers. It is public member function. This function:

Syntax:
void swap(unordered_set &another_unordered_set);
Parameters:

It receives

another_unordered_set

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_set swap()

function :-

Example 1: CPP
#include <iostream>
#include <string>
#include <unordered_set>

using namespace std;

int main()
{

    // sets the values in two container
    unordered_set<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;
}
Output:
before swap :- 
1st container : FOR GEEKS
2nd container : GEEKS

after swap :- 
1st container : GEEKS
2nd container : FOR GEEKS
Example 2: CPP
#include <iostream>
#include <string>
#include <unordered_set>

using namespace std;

int main()
{

    // sets the values in two container
    unordered_set<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;
}
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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4