unordered_map operator= in C++ STL
The ‘=’ is an operator in C++ STL which copies (or moves) an unordered_map to another unordered_map and unordered_map::operator= is the corresponding operator function. There are three versions of this function.
- The first version takes reference of an unordered_map as an argument and copies it to an unordered_map.
- The second version performs a move assignment i.e it moves the content of an unordered_map to another unordered_map.
- The third version assigns contents of an initializer list to an unordered_map.
Syntax
ump.operator= ( unordered_map& ump )
ump.operator= ( unordered_map&& ump )
ump.operator= ( initializer list )
Parameters:
- The first version takes the reference of an unordered_map as argument.
- The second version takes the r-value reference of an unordered_map as argument.
- The third version takes an initializer list as argument.
Return value: All of them returns the value of this pointer(*this).
Below program illustrates the unordered_map::operator= in C++.
Example
#include <bits/stdc++.h>
using namespace std;
template < class T>
T merge(T a, T b)
{
T t(a);
t.insert(b.begin(), b.end());
return t;
}
int main()
{
unordered_map< int , int > sample1, sample2, sample3;
sample1 = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
sample2 = { { 7, 8 }, { 9, 10 }, { 11, 12 } };
sample3 = merge(sample1, sample2);
sample1 = sample3;
for ( auto & it : sample1)
cout << it.first << " : " << it.second << endl;
for ( auto & it : sample2)
cout << it.first << " : " << it.second << endl;
for ( auto & it : sample3)
cout << it.first << " : " << it.second << endl;
return 0;
}
|
Output:
7 : 8
9 : 10
11 : 12
1 : 2
3 : 4
5 : 6
11 : 12
9 : 10
7 : 8
7 : 8
9 : 10
11 : 12
1 : 2
3 : 4
5 : 6
Last Updated :
14 Dec, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...