C++ unordered_map - unordered_map() Function
The C++ unordered_map::unordered_map function is used to construct a unordered_map object, initializing its contents depending on the version of constructor used:
Syntax
//default version - construct an empty //container with no elements explicit unordered_map ( size_type n = /* see below */, const hasher& hf = hasher(), const key_equal& eql = key_equal(), const allocator_type& alloc = allocator_type() ); explicit unordered_map ( const allocator_type& alloc ); //range version - Constructs a container with //elements as the range [first,last) template <class InputIterator> unordered_map ( InputIterator first, InputIterator last, size_type n = /* see below */, const hasher& hf = hasher(), const key_equal& eql = key_equal(), const allocator_type& alloc = allocator_type() ); //copy version - copies all elements //of ust into the container unordered_map ( const unordered_map& ump ); unordered_map ( const unordered_map& ump, const allocator_type& alloc ); //move version - moves elements of ust //into the container unordered_map ( unordered_map&& ump ); unordered_map ( unordered_map&& ump, const allocator_type& alloc ); //initializer list version - copies all //elements of il into the container unordered_map (initializer_list<value_type> il, size_type n = /* see below */, const hasher& hf = hasher(), const key_equal& eql = key_equal(), const allocator_type& alloc = allocator_type() );
//default version - construct an empty //container with no elements unordered_map(); explicit unordered_map ( size_type n, const hasher& hf = hasher(), const key_equal& eql = key_equal(), const allocator_type& alloc = allocator_type() ); explicit unordered_map ( const allocator_type& alloc ); unordered_map ( size_type n, const allocator_type& alloc ); unordered_map ( size_type n, const hasher& hf, const allocator_type& alloc ); //range version - Constructs a container with //elements as the range [first,last) template <class InputIterator> unordered_map ( InputIterator first, InputIterator last, size_type n = /* see below */, const hasher& hf = hasher(), const key_equal& eql = key_equal(), const allocator_type& alloc = allocator_type() ); template <class InputIterator> unordered_map ( InputIterator first, InputIterator last, size_type n, const allocator_type& alloc ); template <class InputIterator> unordered_map ( InputIterator first, InputIterator last, size_type n, const hasher& hf, const allocator_type& alloc ); //copy version - copies all elements //of ust into the container unordered_map ( const unordered_map& ump ); unordered_map ( const unordered_map& ump, const allocator_type& alloc ); //move version - moves elements of ust //into the container unordered_map ( unordered_map&& ump ); unordered_map ( unordered_map&& ump, const allocator_type& alloc ); //initializer list version - copies all //elements of il into the container unordered_map (initializer_list<value_type> il, size_type n = /* see below */, const hasher& hf = hasher(), const key_equal& eql = key_equal(), const allocator_type& alloc = allocator_type() ); unordered_map ( initializer_list<value_type> il, size_type n, const allocator_type& alloc ); unordered_map ( initializer_list<value_type> il, size_type n, const hasher& hf, const allocator_type& alloc );
Parameters
alloc |
Specify the Allocator object. The container keeps and uses an internal copy of this allocator. |
n |
It contains information about minimum number of initial buckets. |
hf |
It is a hasher function object. |
eql |
The comparison function object, that returns true if the two container object keys passed as arguments are equal. |
first |
Specify initial position of the input iterator of the range. The range used is [first,last). |
last |
Specify final position of the input iterator of the range. The range used is [first,last). |
ump |
Specify an unordered_map object of same type. |
il |
Specify an initializer_list object. |
Return Value
Constructor never returns value.
Time Complexity
Constant i.e, Θ(1), for default version and move version.
For all the other cases, Average case: Linear i.e, Θ(n), Worst case: Quadratic i.e, Θ(n2).
Example: using default version
In the example below, the unordered_map::unordered_map function is used to construct an unordered_map object.
#include <iostream> #include <unordered_map> using namespace std; int main (){ //default version - construct an empty unordered_map unordered_map<int, string> uMap; unordered_map<int, string>::iterator it; cout<<"uMap contains:\n"; for(it = uMap.begin(); it != uMap.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; //populating unordered_map uMap[101] = "John"; uMap[102] = "Marry"; cout<<"\nuMap 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: uMap contains: 102 Marry 101 John
Example: using range and copy version
An unordered_map can also be constructed using range or copy version. 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 unordered_map uMap1[101] = "John"; uMap1[102] = "Marry"; uMap1[103] = "Sam"; //range version - construct uMap2 using range unordered_map<int, string> uMap2(uMap1.begin(), uMap1.end()); //copy version - construct uMap3 from uMap1 unordered_map<int, string> uMap3(uMap1); cout<<"uMap2 contains:\n"; for(it = uMap2.begin(); it != uMap2.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; cout<<"\nuMap3 contains:\n"; for(it = uMap3.begin(); it != uMap3.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; return 0; }
The output of the above code will be:
uMap2 contains: 101 John 102 Marry 103 Sam uMap3 contains: 103 Sam 102 Marry 101 John
Example: using move version
Using the move version of unordered_map, 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 unordered_map uMap1[101] = "John"; uMap1[102] = "Marry"; uMap1[103] = "Sam"; 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(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 Sam 102 Marry 101 John uMap1 contains: uMap2 contains: 103 Sam 102 Marry 101 John
Example: using initializer list version
The initializer list can also be used to assign values into an unordered_map container. Consider the example below:
#include <iostream> #include <unordered_map> using namespace std; int main (){ //creating initializer list initializer_list<pair<const int, string>> ilist = {{101, "John"}, {102, "Marry"}}; //initializer list version - copies all //elements of ilist into the container unordered_map<int, string> uMap(ilist); unordered_map<int, string>::iterator it; 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