map operator= in C++ STL
Last Updated :
12 Jun, 2023
The map::operator= is a built function in C++ STL which assigns contents of a container to a different container, replacing its current content.
Syntax:
map1_name = map2_name
Parameters: The map on the left is the container in which the map on the right is to be assigned by destroying the elements of map1.
Return Value: This function does not return anything.
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
map< int , int > mp, copymp;
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 4, 50 });
copymp = mp;
cout << "\nThe map mp is : \n";
cout << "KEY\tELEMENT\n";
for ( auto itr = mp.begin(); itr != mp.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n' ;
}
cout << "\nThe map copymap is : \n";
cout << "KEY\tELEMENT\n";
for ( auto itr = copymp.begin(); itr != copymp.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n' ;
}
return 0;
}
|
Output:
The map mp is :
KEY ELEMENT
1 40
2 30
4 50
The map copymap is :
KEY ELEMENT
1 40
2 30
4 50
Time Complexity : O(n*log n) , Where n is the number of inserted elements
Auxiliary Space : O(n)
Share your thoughts in the comments
Please Login to comment...