C++ Standard Library C++ STL Library

C++ map - operator=() Function



The C++ map::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
map& operator= (const map& x);
//copy version - copies all elements
//of x into the container
map& operator= (const map& x);

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

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

Parameters

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

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

  //populating map1
  map1[101] = "John";
  map1[102] = "Marry";
  map1[103] = "Kim";

  //copying all content of map1 into map2
  map<int, string> map2;
  map2 = map1;

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

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

  return 0;
}

The output of the above code will be:

map1 contains:
101 John
102 Marry
103 Kim

map2 contains:
101 John
102 Marry
103 Kim

Example: using move version

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

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

  //populating map1
  map1[101] = "John";
  map1[102] = "Marry";
  map1[103] = "Kim";

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

  //moving all content of map1 into map2
  map<int, string> map2;
  map2 = move(map1);

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

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

  return 0;
}

The output of the above code will be:

map1 contains:
101 John
102 Marry
103 Kim

map1 contains:

map2 contains:
101 John
102 Marry
103 Kim

Example: using initializer list version

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

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

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

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

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

  return 0;
}

The output of the above code will be:

MyMap contains:
101 John
102 Marry

❮ C++ <map> Library