C++ Standard Library C++ STL Library

C++ multimap - operator=() Function



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

Syntax

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

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

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

Parameters

x Specify a 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.
  • Up to logarithmic i.e, Θ(nlog(n)). Linear i.e, Θ(n) if il is already sorted.

Example: using copy version

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

#include <iostream>
#include <map>
using namespace std;
 
int main (){
  multimap<int, string> mmap1;
  multimap<int, string>::iterator it;

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

  //copying all content of mmap1 into mmap2
  multimap<int, string> mmap2;
  mmap2 = mmap1;

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

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

  return 0;
}

The output of the above code will be:

mmap1 contains:
101 John
102 Marry
103 Kim

mmap2 contains:
101 John
102 Marry
103 Kim

Example: using move version

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

#include <iostream>
#include <map>
using namespace std;
 
int main (){
  multimap<int, string> mmap1;
  multimap<int, string>::iterator it;

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

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

  //moving all content of mmap1 into mmap2
  multimap<int, string> mmap2;
  mmap2 = move(mmap1);

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

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

  return 0;
}

The output of the above code will be:

mmap1 contains:
101 John
102 Marry
103 Kim

mmap1 contains:

mmap2 contains:
101 John
102 Marry
103 Kim

Example: using initializer list version

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

#include <iostream>
#include <map>
using namespace std;
 
int main (){
  //creating empty multimap
  multimap<int, string> mmap;
  multimap<int, string>::iterator it;

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

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

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

  return 0;
}

The output of the above code will be:

mmap contains:
101 John
102 Marry

❮ C++ <map> Library