unordered_map():unordered_map(size_type(/* unspecified */)){} (1)(since C++11)
(until C++20)unordered_map();(since C++20)explicitunordered_map(size_typebucket_count,constHash&hash=Hash(),constkey_equal&equal=key_equal(),constAllocator&alloc=Allocator()); (2) (since C++11)unordered_map(size_typebucket_count,constAllocator&alloc):unordered_map(bucket_count,Hash(),key_equal(),alloc){} (3) (since C++14)unordered_map(size_typebucket_count,constHash&hash,constAllocator&alloc):unordered_map(bucket_count,hash,key_equal(),alloc){} (4)(since C++14)explicitunordered_map(constAllocator&alloc); (5) (since C++11)template<classInputIt>unordered_map(InputItfirst,InputItlast,size_typebucket_count=/* unspecified */,constHash&hash=Hash(),constkey_equal&equal=key_equal(),constAllocator&alloc=Allocator()); (6) (since C++11)template<classInputIt>unordered_map(InputItfirst,InputItlast,size_typebucket_count,constAllocator&alloc):unordered_map(first,last,bucket_count,Hash(),key_equal(),alloc){} (7) (since C++14)template<classInputIt>unordered_map(InputItfirst,InputItlast,size_typebucket_count,constHash&hash,constAllocator&alloc):unordered_map(first,last,bucket_count,hash,key_equal(),alloc){} (8) (since C++14)unordered_map(constunordered_map&other); (9) (since C++11)unordered_map(constunordered_map&other,constAllocator&alloc); (10) (since C++11)unordered_map(unordered_map&&other); (11) (since C++11)unordered_map(unordered_map&&other,constAllocator&alloc); (12) (since C++11)unordered_map(std::initializer_list<value_type>init,size_typebucket_count=/* unspecified */,constHash&hash=Hash(),constkey_equal&equal=key_equal(),constAllocator&alloc=Allocator()); (13)(since C++11)unordered_map(std::initializer_list<value_type>init,size_typebucket_count,constAllocator&alloc):unordered_map(init,bucket_count,Hash(),key_equal(),alloc){} (14) (since C++14)unordered_map(std::initializer_list<value_type>init,size_typebucket_count,constHash&hash,constAllocator&alloc):unordered_map(init,bucket_count,hash,key_equal(),alloc){} (15) (since C++14)template<container-compatible-range<value_type>R>unordered_map(std::from_range_t,R&&rg,size_typebucket_count=/* see description */,constHash&hash=Hash(),constkey_equal&equal=key_equal(),constAllocator&alloc=Allocator()); (16)(since C++23)template<container-compatible-range<value_type>R>unordered_map(std::from_range_t,R&&rg,size_typebucket_count,constAllocator&alloc):unordered_map(std::from_range,std::forward<R>(rg),bucket_count,Hash(),key_equal(),alloc){} (17) (since C++23)template<container-compatible-range<value_type>R>unordered_map(std::from_range_t,R&&rg,size_typebucket_count,constHash&hash,constAlloc&alloc):unordered_map(std::from_range,std::forward<R>(rg),bucket_count,hash,key_equal(),alloc){} (18) (since C++23)Constructs new container from a variety of data sources. Optionally uses user supplied bucket_count as a minimal number of buckets to create, hash as the hash function, equal as the function to compare keys and alloc as the allocator.
1-5) Constructs empty container. Sets
to 1.0. For the default constructor, the number of buckets is unspecified.
6-8) Constructs the container with the contents of the range [first, last). Sets
to 1.0. If multiple elements in the range have keys that compare equivalent, it is unspecified which element is inserted (pending
).
9,10) Copy constructor. Constructs the container with the copy of the contents of other, copies the load factor, the predicate, and the hash function as well. If alloc is not provided, allocator is obtained by calling std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()).
11,12)
. Constructs the container with the contents of other using move semantics. If alloc is not provided, allocator is obtained by move-construction from the allocator belonging to other.
13-15)
. Constructs the container with the contents of the initializer list init, same as unordered_map(init.begin(),init.end()).
16-18) Constructs the container with the contents of rg. If multiple elements in the range have keys that compare equivalent, it is unspecified which element is inserted (pending
).
Parameters
alloc - allocator to use for all memory allocations of this container bucket_count - minimal number of buckets to use on initialization. If it is not specified, an unspecified default value is used hash - hash function to use equal - comparison function to use for all key comparisons of this container first, last - the pair of iterators defining the source
of elements to copy rg - a
, that is, an
whose elements are convertible to
other - another container to be used as source to initialize the elements of the container with init - initializer list to initialize the elements of the container with Type requirements -InputIt must meet the requirements of
. Complexity
1-5) Constant.
6-8) Average case linear (i.e. O(N), where N is std::distance(first,last)), worst case quadratic, i.e. O(N2).
9,10) Linear in size of other.
11,12) Constant. If alloc is given and alloc!=other.get_allocator(), then linear.
13-15) Average case O(N) (N is std::size(init)), worst case O(N2).
16-18) Average case O(N) (N is ranges::distance(rg)), worst case O(N2).
Exceptions
Calls to Allocator::allocate may throw.
Notes
After container move construction (overload (
)), references, pointers, and iterators that originally refer to elements in other remain valid, but refer to elements that are now in *this. The current standard makes this guarantee via the blanket statement in
, and a more direct guarantee is under consideration via
.
Although not formally required until C++23, some implementations have already put the template parameter Allocator into
in earlier modes.
macroValueStdFeature
(C++23)
construction and insertion; overloads (
)Example
Run this code
#include<bitset>#include<string>#include<unordered_map>#include<utility>#include<vector>structKey{std::stringfirst;std::stringsecond;};structKeyHash{std::size_toperator()(constKey&k)const{returnstd::hash<std::string>()(k.first)^(std::hash<std::string>()(k.second)<<1);}};structKeyEqual{booloperator()(constKey&lhs,constKey&rhs)const{returnlhs.first==rhs.first&&lhs.second==rhs.second;}};structFoo{Foo(intval_):val(val_){}intval;booloperator==(constFoo&rhs)const{returnval==rhs.val;}};template<>structstd::hash<Foo>{std::size_toperator()(constFoo&f)const{returnstd::hash<int>{}(f.val);}};intmain(){// default constructor: empty mapstd::unordered_map<std::string,std::string>m1;// list constructorstd::unordered_map<int,std::string>m2={{1,"foo"},{3,"bar"},{2,"baz"}};// copy constructorstd::unordered_map<int,std::string>m3=m2;// move constructorstd::unordered_map<int,std::string>m4=std::move(m2);// range constructorstd::vector<std::pair<std::bitset<8>,int>>v={{0x12,1},{0x01,-1}};std::unordered_map<std::bitset<8>,double>m5(v.begin(),v.end());// Option 1 for a constructor with a custom Key type// Define the KeyHash and KeyEqual structs and use them in the templatestd::unordered_map<Key,std::string,KeyHash,KeyEqual>m6={{{"John","Doe"},"example"},{{"Mary","Sue"},"another"}};// Option 2 for a constructor with a custom Key type.// Define a const == operator for the class/struct and specialize std::hash// structure in the std namespacestd::unordered_map<Foo,std::string>m7={{Foo(1),"One"},{2,"Two"},{3,"Three"}};// Option 3: Use lambdas// Note that the initial bucket count has to be passed to the constructorstructGoo{intval;};autohash=[](constGoo&g){returnstd::hash<int>{}(g.val);};autocomp=[](constGoo&l,constGoo&r){returnl.val==r.val;};std::unordered_map<Goo,double,decltype(hash),decltype(comp)>m8(10,hash,comp);}Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++11 the default constructor (1) was explicit made non-explicit
C++11 the semantics of overload (
) was not specified specified See also