map operator= in C++ STL


In this article we will be discussing the working, syntax and example of map equal ‘=’ operator in C++ STL.

What is a Map in C++ STL?

Maps are the associative container, which facilitates to store the elements formed by a combination of key value and mapped value in a specific order. In a map container the data is internally always sorted with the help of its associated keys. The values in the map container are accessed by its unique keys.

What is a map equal to ‘=’ operator?

map::operator= is an equal to operator. This operator is used to copy the elements from one container to another container, by overwriting the current content of the container.

Syntax

Map_name.max_size();

Parameter

There is a map on the left side of the operator and another map on the right side of the container. The contents of the right side are copied to the map on the left side.

Return value

There is no return value of an operator

Example

Input

map<char, int> newmap, themap;
newmap.insert({1, 20});
newmap.insert({2, 30});
themap = newmap

Output

themap = 1:20

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   map<int, int> TP, temp;
   TP.insert({ 2, 20 });
   TP.insert({ 1, 10 });
   TP.insert({ 3, 30 });
   TP.insert({ 4, 40 });
   TP.insert({ 6, 50 });
   temp = TP;
   cout<<"\nData in map TP is: \n";
   cout << "KEY\tELEMENT\n";
   for (auto i = TP.begin(); i!= TP.end(); ++i) {
      cout << i->first << '\t' << i->second << '\n';
   }
   cout << "\nData in copied map temp is : \n";
   cout << "KEY\tELEMENT\n";
   for (auto i = TP.begin(); i!= TP.end(); ++i) {
      cout << i->first << '\t' << i->second << '\n';
   }
   return 0;
}

Output

Data in map TP is:
MAP_KEY    MAP_ELEMENT
1             10
2             20
3             30
4             40
6             50
Data in copied map temp is :
MAP_KEY    MAP_ELEMENT
1             10
2             20
3             30
4             40
6             50

Updated on: 15-Apr-2020

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements