C++ Standard Library C++ STL Library

C++ unordered_multiset - operator=() Function



The C++ unordered_multiset::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_multiset& operator= (const unordered_multiset& x);

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

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

Parameters

x Specify a unordered_multiset 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_multiset::operator= function is used to assign new values to the given unordered_multiset.

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main (){
  unordered_multiset<int> uMSet1{15, 5, 23, 78, 41};
  unordered_multiset<int>::iterator it;

  //copying all content of uMSet1 into uMSet2
  unordered_multiset<int> uMSet2;
  uMSet2 = uMSet1;

  cout<<"uMSet1 contains:";
  for(it = uMSet1.begin(); it != uMSet1.end(); ++it)
    cout<<" "<<*it;

  cout<<"\nuMSet2 contains:";
  for(it = uMSet2.begin(); it != uMSet2.end(); ++it)
    cout<<" "<<*it;

  return 0;
}

The output of the above code will be:

uMSet1 contains: 41 78 23 5 15
uMSet2 contains: 41 78 23 5 15

Example: using move version

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

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main (){
  unordered_multiset<int> uMSet1{15, 5, 23, 78, 41};
  unordered_multiset<int>::iterator it;

  cout<<"uMSet1 contains:";
  for(it = uMSet1.begin(); it != uMSet1.end(); ++it)
    cout<<" "<<*it;

  //moving all content of uMSet1 into uMSet2
  unordered_multiset<int> uMSet2;
  uMSet2 = move(uMSet1);

  cout<<"\nuMSet1 contains:";
  for(it = uMSet1.begin(); it != uMSet1.end(); ++it)
    cout<<" "<<*it;

  cout<<"\nuMSet2 contains:";
  for(it = uMSet2.begin(); it != uMSet2.end(); ++it)
    cout<<" "<<*it;

  return 0;
}

The output of the above code will be:

uMSet1 contains: 41 78 23 5 15
uMSet1 contains:
uMSet2 contains: 41 78 23 5 15

Example: using initializer list version

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

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main (){
  //creating empty unordered_multiset
  unordered_multiset<int> uMSet;
  unordered_multiset<int>::iterator it;

  //creating initializer list
  initializer_list<int> ilist = {15, 5, 23, 78, 41};

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

  cout<<"uMSet contains:";
  for(it = uMSet.begin(); it != uMSet.end(); ++it)
    cout<<" "<<*it;

  return 0;
}

The output of the above code will be:

uMSet contains: 78 23 5 41 15

❮ C++ <unordered_set> Library