unordered_multimap operator= in C++

The unordered_multimap::operator= is a built-in function in C++ STL which does three types of tasks which are explained below.

  1. Syntax (copying elements from different container) :
    unordered_multimap_name1 operator= (unordered_multimap_name2)

    Parameters: The function does not accepts any parameter. The container to the right is the one from which the elements are to be copied to the container in the left.

    Return Value: It does not return anything.

    Below program illustrates the above function:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to illustrate the
    // unordered_multimap::operator=
    #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 });
      
        cout << "Key and Elements of Sample1 before copy  are:";
        for (auto it = sample1.begin(); it != sample1.end(); it++) {
            cout << "{" << it->first << ", " << it->second << "} ";
        }
      
        cout << "\nThe size of sample2 before copy: "
             << sample2.size();
      
        // opeator= to copy
        sample2 = sample1;
      
        cout << "\nKey and Elements of Sample2 after copy 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 copy  are:{50, 500} {10, 100} 
    The size of sample2 before copy: 0
    Key and Elements of Sample2 after copy are: {50, 500} {10, 100}
    
  2. Syntax (For moving elements from different container):
    unordered_multimap_name1 operator= (unordered_multimap_name2)

    Parameters: The function does not accepts any parameter. The container to the right is the one from which the elements are to be moved to the container in the left. The elements in the right container are destroyed after operator= is used.

    Return Value: It does not return anything.

    Below program illustrates the above function:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to illustrate the
    // unordered_multimap::operator=
    #include <bits/stdc++.h>
    using namespace std;
      
    // Function to merge two lists
    unordered_multimap<char, char> merge(unordered_multimap<char, char> a,
                                         unordered_multimap<char, char> b)
    {
        unordered_multimap<char, char> temp(a);
        temp.insert(b.begin(), b.end());
        return temp;
    }
    int main()
    {
      
        // declaration
        unordered_multimap<char, char> sample1, sample2, sample3;
      
        // 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 are: ";
        for (auto it = sample1.begin(); it != sample1.end(); it++) {
            cout << "{" << it->first << ", " << it->second << "} ";
        }
      
        cout << "\nKey and Elements of Sample2 are: ";
        for (auto it = sample2.begin(); it != sample2.end(); it++) {
            cout << "{" << it->first << ", " << it->second << "} ";
        }
      
        // merging and moved
        sample3 = merge(sample1, sample2);
        sample1 = sample3;
      
        cout << "\n\nKey and Elements of Sample1 are: ";
        for (auto it = sample1.begin(); it != sample1.end(); it++) {
            cout << "{" << it->first << ", " << it->second << "} ";
        }
      
        return 0;
    }

    chevron_right

    
    

    Output:

    Key and Elements of Sample1 are: {g, G} {a, A} 
    Key and Elements of Sample2 are: {d, D} {b, B} {c, C} 
    
    Key and Elements of Sample1 are: {c, C} {b, B} {d, D} {a, A} {g, G}
    
  3. Syntax (For assigning the elements from different list):
    unordered_multimap_name1 operator= (intitializer_list il)

    Parameters: It does not accepts any parameter, it has the list to the right which will be assigned to the container.

    Return Value: It does not returns anything.

    Below program illustrates the above function:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to illustrate the
    // unordered_multimap::operator=
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
      
        // declaration by using operator=
        unordered_multimap<int, int> sample1 = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
      
        cout << "Key and Elements of Sample1 are: ";
        for (auto it = sample1.begin(); it != sample1.end(); it++) {
            cout << "{" << it->first << ", " << it->second << "} ";
        }
      
        // declaration by using operator=
        unordered_multimap<char, char> sample2 = { { 'a', 'A' }, { 'b', 'B' }, { 'c', 'C' } };
      
        cout << "\n\nKey and Elements of Sample1 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 are: {5, 6} {3, 4} {1, 2} 
    
    Key and Elements of Sample1 are: {c, C} {b, B} {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.