C++ Standard Library C++ STL Library

C++ unordered_multimap - operator=() Function



The C++ unordered_multimap::operator= function is used to assign new content to the container by replacing the current content.

Syntax

//copy version - copies all elements
//of x into the container
unordered_multimap& operator= (const unordered_multimap& x);

//move version - moves elements of x
//into the container
unordered_multimap& operator= (unordered_multimap&& x);

//initializer list version - copies all 
//elements of il into the container
unordered_multimap& operator= (initializer_list<value_type> il);

Parameters

x Specify a unordered_multimap object of same type.
il Specify an initializer_list object.

Return Value

Returns *this.

Time Complexity

  • Linear i.e, Θ(n) for copy version and move version.
  • On average: Linear i.e, Θ(n). Worst case: quadratic i.e, Θ(n2).

Example: using copy version

In the example below, the unordered_multimap::operator= function is used to assign new content to the given unordered_multimap.

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_multimap<int, string> uMMap1;
  unordered_multimap<int, string>::iterator it;

  //populating uMMap1
  uMMap1.insert(pair<int, string>(101, "John"));
  uMMap1.insert(pair<int, string>(102, "Marry"));
  uMMap1.insert(pair<int, string>(103, "Kim"));

  //copying all content of uMMap1 into uMMap2
  unordered_multimap<int, string> uMMap2;
  uMMap2 = uMMap1;

  cout<<"uMMap1 contains:\n";
  for(it = uMMap1.begin(); it != uMMap1.end(); ++it)
    cout<<it->first<<" "<<it->second<<"\n";

  cout<<"\nuMMap2 contains:\n";
  for(it = uMMap2.begin(); it != uMMap2.end(); ++it)
    cout<<it->first<<" "<<it->second<<"\n";

  return 0;
}

The output of the above code will be:

uMMap1 contains:
103 Kim
102 Marry
101 John

uMMap2 contains:
103 Kim
102 Marry
101 John

Example: using move version

Using the move version of operator=, the content of one unordered_multimap can be moved to another unordered_multimap. Consider the following example:

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_multimap<int, string> uMMap1;
  unordered_multimap<int, string>::iterator it;

  //populating uMMap1
  uMMap1.insert(pair<int, string>(101, "John"));
  uMMap1.insert(pair<int, string>(102, "Marry"));
  uMMap1.insert(pair<int, string>(103, "Kim"));

  cout<<"uMMap1 contains:\n";
  for(it = uMMap1.begin(); it != uMMap1.end(); ++it)
    cout<<it->first<<" "<<it->second<<"\n";

  //moving all content of uMMap1 into uMMap2
  unordered_multimap<int, string> uMMap2;
  uMMap2 = move(uMMap1);

  cout<<"\nuMMap1 contains:\n";
  for(it = uMMap1.begin(); it != uMMap1.end(); ++it)
    cout<<it->first<<" "<<it->second<<"\n";

  cout<<"\nuMMap2 contains:\n";
  for(it = uMMap2.begin(); it != uMMap2.end(); ++it)
    cout<<it->first<<" "<<it->second<<"\n";

  return 0;
}

The output of the above code will be:

uMMap1 contains:
103 Kim
102 Marry
101 John

uMMap1 contains:

uMMap2 contains:
103 Kim
102 Marry
101 John

Example: using initializer list version

The initializer list can also be used to assign values into a unordered_multimap container. Consider the example below:

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  //creating empty unordered_multimap
  unordered_multimap<int, string> uMMap;
  unordered_multimap<int, string>::iterator it;

  //creating initializer list
  initializer_list<pair<const int, string>> ilist = {{101, "John"}, {102, "Marry"}};

  //assigning values of uMMap using ilist
  uMMap = ilist;

  cout<<"uMMap contains:\n";
  for(it = uMMap.begin(); it != uMMap.end(); ++it)
    cout<<it->first<<" "<<it->second<<"\n";

  return 0;
}

The output of the above code will be:

uMMap contains:
102 Marry
101 John

❮ C++ <unordered_map> Library