C++ unordered_multimap - unordered_multimap() Function
The C++ unordered_multimap::unordered_multimap function is used to construct a unordered_multimap object, initializing its contents depending on the version of constructor used:
Syntax
//default version - construct an empty //container with no elements explicit unordered_multimap ( size_type n = /* see below */, const hasher& hf = hasher(), const key_equal& eql = key_equal(), const allocator_type& alloc = allocator_type() ); explicit unordered_multimap ( const allocator_type& alloc ); //range version - Constructs a container with //elements as the range [first,last) template <class InputIterator> unordered_multimap ( 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_multimap ( const unordered_multimap& umm ); unordered_multimap ( const unordered_multimap& umm, const allocator_type& alloc ); //move version - moves elements of ust //into the container unordered_multimap ( unordered_multimap&& umm ); unordered_multimap ( unordered_multimap&& umm, const allocator_type& alloc ); //initializer list version - copies all //elements of il into the container unordered_multimap (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_multimap(); explicit unordered_multimap ( size_type n, const hasher& hf = hasher(), const key_equal& eql = key_equal(), const allocator_type& alloc = allocator_type() ); explicit unordered_multimap ( const allocator_type& alloc ); unordered_multimap ( size_type n, const allocator_type& alloc ); unordered_multimap ( 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_multimap ( 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_multimap ( InputIterator first, InputIterator last, size_type n, const allocator_type& alloc ); template <class InputIterator> unordered_multimap ( 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_multimap ( const unordered_multimap& umm ); unordered_multimap ( const unordered_multimap& umm, const allocator_type& alloc ); //move version - moves elements of ust //into the container unordered_multimap ( unordered_multimap&& umm ); unordered_multimap ( unordered_multimap&& umm, const allocator_type& alloc ); //initializer list version - copies all //elements of il into the container unordered_multimap (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_multimap ( initializer_list<value_type> il, size_type n, const allocator_type& alloc ); unordered_multimap ( 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_multimap 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_multimap::unordered_multimap function is used to construct an unordered_multimap object.
#include <iostream> #include <unordered_map> using namespace std; int main (){ //default version - construct an empty unordered_multimap unordered_multimap<string, string> uMMap; unordered_multimap<string, string>::iterator it; cout<<"uMMap contains:\n"; for(it = uMMap.begin(); it != uMMap.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; //populating unordered_multimap uMMap.insert(pair<string, string>("CAN", "Montreal")); uMMap.insert(pair<string, string>("IND", "Delhi")); cout<<"\nuMMap contains:\n"; for(it = uMMap.begin(); it != uMMap.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; return 0; }
The output of the above code will be:
uMMap contains: uMMap contains: IND Delhi CAN Montreal
Example: using range and copy version
An unordered_multimap can also be constructed using range or copy version. Consider the following example:
#include <iostream> #include <unordered_map> using namespace std; int main (){ unordered_multimap<string, string> uMMap1; unordered_multimap<string, string>::iterator it; //populating unordered_multimap uMMap1.insert(pair<string, string>("CAN", "Montreal")); uMMap1.insert(pair<string, string>("IND", "Delhi")); //range version - construct uMMap2 using range unordered_multimap<string, string> uMMap2(uMMap1.begin(), uMMap1.end()); //copy version - construct uMMap3 from uMMap1 unordered_multimap<string, string> uMMap3(uMMap1); cout<<"uMMap2 contains:\n"; for(it = uMMap2.begin(); it != uMMap2.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; cout<<"\nuMMap3 contains:\n"; for(it = uMMap3.begin(); it != uMMap3.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; return 0; }
The output of the above code will be:
uMMap2 contains: CAN Montreal IND Delhi uMMap3 contains: IND Delhi CAN Montreal
Example: using move version
Using the move version of unordered_multimap, the content of one unordered_multimap can be moved to another unordered_multimap. Consider the following example:
#include <iostream> #include <unordered_map> using namespace std; int main (){ unordered_multimap<string, string> uMMap1; unordered_multimap<string, string>::iterator it; //populating unordered_multimap uMMap1.insert(pair<string, string>("CAN", "Montreal")); uMMap1.insert(pair<string, string>("IND", "Delhi")); cout<<"uMMap1 contains:\n"; for(it = uMMap1.begin(); it != uMMap1.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; //moving all content of uMMap1 into uMMap2 unordered_multimap<string, string> uMMap2(move(uMMap1)); cout<<"\nuMMap1 contains:\n"; for(it = uMMap1.begin(); it != uMMap1.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; cout<<"\nuMMap2 contains:\n"; for(it = uMMap2.begin(); it != uMMap2.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; return 0; }
The output of the above code will be:
uMMap1 contains: IND Delhi CAN Montreal uMMap1 contains: uMMap2 contains: IND Delhi CAN Montreal
Example: using initializer list version
The initializer list can also be used to assign values into an unordered_multimap container. Consider the example below:
#include <iostream> #include <unordered_map> using namespace std; int main (){ //creating initializer list initializer_list<pair<const string, string>> ilist = {{"CAN", "Montreal"}, {"IND", "Delhi"}}; //initializer list version - copies all //elements of ilist into the container unordered_multimap<string, string> uMMap(ilist); unordered_multimap<string, string>::iterator it; cout<<"uMMap contains:\n"; for(it = uMMap.begin(); it != uMMap.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; return 0; }
The output of the above code will be:
uMMap contains: IND Delhi CAN Montreal
❮ C++ <unordered_map> Library