In this article we will be discussing the working, syntax and example of map equal ‘=’ operator 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.
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.
Map_name.max_size();
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.
There is no return value of an operator
map<char, int> newmap, themap; newmap.insert({1, 20}); newmap.insert({2, 30}); themap = newmap
themap = 1:20
#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; }
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