C++ Standard Library C++ STL Library

C++ - <unordered_map>



The C++ <unordered_map> header file defines the unordered_map and unordered_multimap container classes:

unordered_map class

An unordered map is an associative container that stores data sequence in key-value pair in no particular order, which allows fast retrieval of an element based on its keys. In a unordered_map, no two mapped values can have the same key. The data type of key and mapped value may differ and can be represented as:

typedef pair<const Key, T> value_type;

Each key in a unordered_map is unique which can be inserted or deleted in the unordered_map but can not be altered. Values associated with keys can be changed. The value in a unordered_map can be directly accessed by specifying corresponding key using the operator [].

Syntax

template < class Key,                                     // key_type
           class T,                                       // mapped_type
           class Hash = hash<Key>,                        // hasher
           class Pred = equal_to<Key>,                    // key_equal
           class Alloc = allocator<pair<const Key,T>>     // allocator_type
           > class unordered_map;

Parameters

Key Type of the key.
T Type of the mapped value.
Hash A unary function that takes two elements of unordered_map as arguments and returns a unique value of type size_t based on it.
Pred A binary predicate that takes two elements of unordered_map as arguments and returns a bool. It is used to determine whether two elements are equivalent or not.
Alloc Type of the allocator object used to define the storage allocation model, default:allocator.

Member Types

Member typesDefinition
key_type T (First template parameter)
value_type T (First template parameter)
hasher Hash (Second template parameter), default: hash<key_type>
key_equal Pred (Third template parameter), default: equal_to<key_type>
allocator_type Alloc (Fourth template parameter), default: allocator<value_type>
reference Alloc::reference
const_reference Alloc::const_reference
pointer Alloc::pointer, default: value_type*
const_pointer Alloc::const_pointer, default: value_type*
iterator a forward iterator to const value_type, convertible to const_iterator
const_iterator a forward iterator to const value_type
local_iterator a forward iterator to const value_type, convertible to const_local_iterator
const_local_iterator a forward iterator to const value_type
difference_type ptrdiff_t
size_type size_t

C++ unordered_map - Member Functions

The C++ unordered_map container has a number of member functions which are listed below:

FunctionsDescription
unordered_map() Construct a unordered_map object.
~unordered_map() Destroys container by deallocating container memory.
operator=() Assign content to a unordered_map.

Capacity

FunctionsDescription
empty() Checks whether the unordered_map is empty or not.
size() Returns the size of the unordered_map.
max_size() Returns the maximum size of the unordered_map.

Element lookup

FunctionsDescription
find() Searches the container for a key and returns the iterator to it if found, else returns the iterator to unordered_map::end.
count() Returns the number of occurrences of a key in the unordered_map container.
equal_range() Returns the range of elements in the unordered_map container with a key that matches with the given value.

Element Access

FunctionsDescription
at() Access an element of the unordered_map.
operator[]() Access an element of the unordered_map.

Iterators

FunctionsDescription
begin() Returns iterator pointing to the first element of the unordered_map.
end() Returns iterator pointing to the past-the-last element of the unordered_map.
cbegin() Returns const_iterator pointing to the first element of the unordered_map.
cend() Returns const_iterator pointing to the past-the-last element of the unordered_map.

Modifiers

FunctionsDescription
clear() Clears all elements of the unordered_map.
erase() Deletes either a single element or range of elements from a unordered_map.
insert() Insert elements in the unordered_map.
swap() Exchanges elements between two unordered_maps.
emplace() Constructs and inserts a new element in the unordered_map if the key is unique.
emplace_hint() Constructs and inserts a new element with hint in the unordered_map if the key is unique.

Buckets

FunctionsDescription
bucket_count() Returns the number of buckets in the unordered_map.
max_bucket_count() Returns the maximum number of buckets that the unordered_map can have.
bucket_size() Returns the number of elements in the specified bucket of the unordered_map.
bucket() Returns the bucket number of the specified element of the unordered_map.

Hash policy

FunctionsDescription
load_factor() Returns current load factor in the unordered_map.
max_load_factor() Returns or sets the maximum load factor in the unordered_map.
rehash() Sets number of buckets in the unordered_map.
reserve() Requests a capacity change in the unordered_map.

Observers

FunctionsDescription
hash_function() Get the hash function object used by unordered_map container.
key_eq() Returns the function which compares keys for equality.
get_allocator() Return a copy of allocator object associated with the unordered_map.

C++ unordered_map - Non-member Functions

FunctionsDescription
operator == Checks whether two unordered_maps are equal or not.
operator != Checks whether two unordered_maps are unequal or not.
swap() Exchanges elements between two unordered_maps.

unordered_multimap class

An unordered multimap is an associative container that stores data sequence in key-value pair in no particular order, which allows fast retrieval of an element based on its keys. Unlike unordered_map or map, in unordered_multimap two mapped values can have the same key. The data type of key and value may differ and can be represented as:

Syntax

template < class Key,                                     // key_type
           class T,                                       // mapped_type
           class Hash = hash<Key>,                        // hasher
           class Pred = equal_to<Key>,                    // key_equal
           class Alloc = allocator<pair<const Key,T>>     // allocator_type
           > class unordered_multimap;

Parameters

Key Type of the key.
T Type of the mapped value.
Hash A unary function that takes two elements of unordered_multimap as arguments and returns a unique value of type size_t based on it.
Pred A binary predicate that takes two elements of unordered_multimap as arguments and returns a bool. It is used to determine whether two elements are equivalent or not.
Alloc Type of the allocator object used to define the storage allocation model, default:allocator.

Member Types

Member typesDefinition
key_type T (First template parameter)
value_type T (First template parameter)
hasher Hash (Second template parameter), default: hash<key_type>
key_equal Pred (Third template parameter), default: equal_to<key_type>
allocator_type Alloc (Fourth template parameter), default: allocator<value_type>
reference Alloc::reference
const_reference Alloc::const_reference
pointer Alloc::pointer, default: value_type*
const_pointer Alloc::const_pointer, default: value_type*
iterator a forward iterator to const value_type, convertible to const_iterator
const_iterator a forward iterator to const value_type
local_iterator a forward iterator to const value_type, convertible to const_local_iterator
const_local_iterator a forward iterator to const value_type
difference_type ptrdiff_t
size_type size_t

C++ unordered_multimap - Member Functions

The C++ unordered_multimap container has a number of member functions which are listed below:

FunctionsDescription
unordered_multimap() Construct a unordered_multimap object.
~unordered_multimap() Destroys container by deallocating container memory.
operator=() Assign content to a unordered_multimap.

Capacity

FunctionsDescription
empty() Checks whether the unordered_multimap is empty or not.
size() Returns the size of the unordered_multimap.
max_size() Returns the maximum size of the unordered_multimap.

Element lookup

FunctionsDescription
find() Searches the container for a key and returns the iterator to it if found, else returns the iterator to unordered_multimap::end.
count() Returns the number of occurrences of a key in the unordered_multimap container.
equal_range() Returns the range of elements in the unordered_multimap container with a key that matches with the given value.

Iterators

FunctionsDescription
begin() Returns iterator pointing to the first element of the unordered_multimap.
end() Returns iterator pointing to the past-the-last element of the unordered_multimap.
cbegin() Returns const_iterator pointing to the first element of the unordered_multimap.
cend() Returns const_iterator pointing to the past-the-last element of the unordered_multimap.

Modifiers

FunctionsDescription
clear() Clears all elements of the unordered_multimap.
erase() Deletes either a single element or range of elements from a unordered_multimap.
insert() Insert elements in the unordered_multimap.
swap() Exchanges elements between two unordered_multimaps.
emplace() Constructs and inserts a new element in the unordered_multimap.
emplace_hint() Constructs and inserts a new element with hint in the unordered_multimap.

Buckets

FunctionsDescription
bucket_count() Returns the number of buckets in the unordered_multimap.
max_bucket_count() Returns the maximum number of buckets that the unordered_multimap can have.
bucket_size() Returns the number of elements in the specified bucket of the unordered_multimap.
bucket() Returns the bucket number of the specified element of the unordered_multimap.

Hash policy

FunctionsDescription
load_factor() Returns current load factor in the unordered_multimap.
max_load_factor() Returns or sets the maximum load factor in the unordered_multimap.
rehash() Sets number of buckets in the unordered_multimap.
reserve() Requests a capacity change in the unordered_multimap.

Observers

FunctionsDescription
hash_function() Get the hash function object used by unordered_multimap container.
key_eq() Returns the function which compares keys for equality.
get_allocator() Return a copy of allocator object associated with the unordered_multimap.

C++ unordered_multimap - Non-member Functions

FunctionsDescription
operator == Checks whether two unordered_multimaps are equal or not.
operator != Checks whether two unordered_multimaps are unequal or not.
swap() Exchanges elements between two unordered_multimaps.