Open In App

unordered_multimap operator= in C++ STL

Last Updated : 14 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The ‘=’ is an operator in C++ STL which copies (or moves) an unordered_multimap to another unordered_multimap and unordered_multimap::operator= is the corresponding operator function. There are three versions of this function.

  1. The first version takes reference of an unordered_multimap as an argument and copies it to an unordered_multimap.
  2. The second version performs a move assignment i.e it moves the content of an unordered_multimap to another unordered_multimap.
  3. The third version assigns contents of an initializer list to an unordered_multimap.

Syntax:

umm.operator= ( unordered_multimap& umm )
umm.operator= ( unordered_multimap&& umm )
umm.operator= ( initializer list )

Parameters:

  1. The first version takes the reference of an unordered_multimap as argument.
  2. The second version takes the r-value reference of an unordered_multimap as argument.
  3. The third version takes an initializer list as argument.

Return value: All of them returns the value of this pointer(*this).
Below program illustrates the unordered_multimap::operator= in C++.
Example




// C++ code to illustrate the method
// unordered_multimap::operator=()
#include <bits/stdc++.h>
  
using namespace std;
  
// merge function
template <class T>
T merge(T a, T b)
{
    T t(a);
    t.insert(b.begin(), b.end());
    return t;
}
  
int main()
{
    unordered_multimap<int, int> sample1, sample2, sample3;
  
    // List initialization
    sample1 = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
    sample2 = { { 7, 8 }, { 9, 10 }, { 11, 12 } };
  
    // Merge both lists
    sample3 = merge(sample1, sample2);
  
    // copy assignment
    sample1 = sample3;
  
    // Print the unordered_multimap list
    for (auto& it : sample1)
        cout << it.first << " : " << it.second << endl;
    cout << endl;
    for (auto& it : sample2)
        cout << it.first << " : " << it.second << endl;
    cout << endl;
    for (auto& it : sample3)
        cout << it.first << " : " << it.second << endl;
    return 0;
}


Output:

7 : 8
9 : 10
11 : 12
1 : 2
3 : 4
5 : 6

11 : 12
9 : 10
7 : 8

7 : 8
9 : 10
11 : 12
1 : 2
3 : 4
5 : 6


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads