C++ unordered_map - operator=() Function
The C++ unordered_map::operator= function is used to assign new content to the container by replacing the current content.
Syntax
//copy version - copies all elements //of x into the container unordered_map& operator= (const unordered_map& x); //move version - moves elements of x //into the container unordered_map& operator= (unordered_map&& x); //initializer list version - copies all //elements of il into the container unordered_map& operator= (initializer_list<value_type> il);
Parameters
x |
Specify a unordered_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.
- On average: Linear i.e, Θ(n). Worst case: quadratic i.e, Θ(n2).
Example: using copy version
In the example below, the unordered_map::operator= function is used to assign new content to the given unordered_map.
#include <iostream> #include <unordered_map> using namespace std; int main (){ unordered_map<int, string> uMap1; unordered_map<int, string>::iterator it; //populating uMap1 uMap1[101] = "John"; uMap1[102] = "Marry"; uMap1[103] = "Kim"; //copying all content of uMap1 into uMap2 unordered_map<int, string> uMap2; uMap2 = uMap1; cout<<"uMap1 contains:\n"; for(it = uMap1.begin(); it != uMap1.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; cout<<"\nuMap2 contains:\n"; for(it = uMap2.begin(); it != uMap2.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; return 0; }
The output of the above code will be:
uMap1 contains: 103 Kim 102 Marry 101 John uMap2 contains: 103 Kim 102 Marry 101 John
Example: using move version
Using the move version of operator=, the content of one unordered_map can be moved to another unordered_map. Consider the following example:
#include <iostream> #include <unordered_map> using namespace std; int main (){ unordered_map<int, string> uMap1; unordered_map<int, string>::iterator it; //populating uMap1 uMap1[101] = "John"; uMap1[102] = "Marry"; uMap1[103] = "Kim"; cout<<"uMap1 contains:\n"; for(it = uMap1.begin(); it != uMap1.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; //moving all content of uMap1 into uMap2 unordered_map<int, string> uMap2; uMap2 = move(uMap1); cout<<"\nuMap1 contains:\n"; for(it = uMap1.begin(); it != uMap1.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; cout<<"\nuMap2 contains:\n"; for(it = uMap2.begin(); it != uMap2.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; return 0; }
The output of the above code will be:
uMap1 contains: 103 Kim 102 Marry 101 John uMap1 contains: uMap2 contains: 103 Kim 102 Marry 101 John
Example: using initializer list version
The initializer list can also be used to assign values into a unordered_map container. Consider the example below:
#include <iostream> #include <unordered_map> using namespace std; int main (){ //creating empty unordered_map unordered_map<int, string> uMap; unordered_map<int, string>::iterator it; //creating initializer list initializer_list<pair<const int, string>> ilist = {{101, "John"}, {102, "Marry"}}; //assigning values of uMap using ilist uMap = ilist; cout<<"uMap contains:\n"; for(it = uMap.begin(); it != uMap.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; return 0; }
The output of the above code will be:
uMap contains: 102 Marry 101 John
❮ C++ <unordered_map> Library