public member function
<unordered_map>

std::unordered_map::unordered_map

empty (default) (1)
explicit unordered_map ( size_type n = /* see below */,
                         const hasher& hf = hasher(),
                         const key_equal& eql = key_equal(),
                         const allocator_type& alloc = allocator_type() );
copy (2)
unordered_map ( const unordered_map& ump );
move (3)
unordered_map ( unordered_map&& ump );
empty+allocator (4)
explicit unordered_map ( const allocator_type& alloc );
copy+allocator (5)
unordered_map ( const unordered_map& ump, const allocator_type& alloc );
move+allocator (6)
unordered_map ( unordered_map&& ump, const allocator_type& alloc );
range (7)
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() );
initializer list (8)
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() );
Construct unordered_map
Constructs an unordered_map container object, initializing its contents depending on the constructor version used:

(1) empty container constructor (default constructor)
Constructs an empty unordered_map object, containing no elements and with a size of zero.
It can construct the container with specific hasher, key_equal and allocator objects, along with a minimum number of hash buckets.
(2) copy constructor
The object is initialized to have the same contents and properties as the ump unordered_map object.
(3) move constructor
The object acquires the contents of the rvalue ump.
(4),(5),(6) allocator constructors
Same as (1), (2), (3), but taking an allocator object as additional argument.
(7) range constructor
Constructs an unordered_map object containing copies of each of the elements in the range [first,last).
(8) initializer list
Initializes the container with the contents of the list.

Parameters

n
Minimum number of initial buckets.
This is not the number of elements in the container, but the minimum number of slots desired for the internal hash table on construction.
Each library has some specific default value for this parameter if none is specified.
Member type size_type is an unsigned integral type.
hf
Hasher function object. A hasher is a function that returns an integral value based on the container object key passed to it as argument.
Member type hasher is defined in unordered_map as an alias of its third template parameter (Hash).
eql
Comparison function object, that returns true if the two container object keys passed as arguments are to be considered equal.
Member type key_equal is defined in unordered_map as an alias of its fourth template parameter (Pred).
alloc
Allocator object to be used instead of constructing a new one.
For class instantiations using their version of the default allocator class template, this parameter is not relevant.
Member type allocator_type is defined in unordered_map as an alias of its fifth template parameter (Alloc).
ump
Another unordered_map object of the same type (with the same class template arguments), whose contents are either copied or moved.
first, last
Input iterators to the initial and final positions in a range. The range used is [first,last), which includes all the elements between first and last, including the element pointed by first but not the element pointed by last.
The function template type can be any type of input iterator.
il
An initializer_list object.
These objects are automatically constructed from initializer list declarators.
Member type value_type is the type of the elements in the container, defined in unordered_map as pair<const key_type,mapped_type>, where member type key_type is an alias of the first template parameter (the key type), and mapped_type is an alias of the second template parameter (the mapped type, T).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// constructing unordered_maps
#include <iostream>
#include <string>
#include <unordered_map>

typedef std::unordered_map<std::string,std::string> stringmap;

stringmap merge (stringmap a,stringmap b) {
  stringmap temp(a); temp.insert(b.begin(),b.end()); return temp;
}

int main ()
{
  stringmap first;                              // empty
  stringmap second ( {{"apple","red"},{"lemon","yellow"}} );       // init list
  stringmap third ( {{"orange","orange"},{"strawberry","red"}} );  // init list
  stringmap fourth (second);                    // copy
  stringmap fifth (merge(third,fourth));        // move
  stringmap sixth (fifth.begin(),fifth.end());  // range

  std::cout << "sixth contains:";
  for (auto& x: sixth) std::cout << " " << x.first << ":" << x.second;
  std::cout << std::endl;

  return 0;
}


Possible output:
sixth contains: apple:red lemon:yellow orange:orange strawberry:red

Complexity

For the empty (1,4) and move (3,6) constructors: constant time.
For the copy (2,5), range (7), and initializer list (8) constructors: average case linear, worst case quadratic.

See also