C++ Library - <unordered_map>



Introduction to unordered_map

Unordered map is dictionary like data structure. It is a sequence of (key, value) pair, where only single value is associated with each unique key. It is often referred as associative array. It enables fast retrieval of individual elements based on their keys. It also implements the direct access operator(subscript operator[]) which allows for direct access of the mapped value using its key value as argument.

Unordered map does not sort its element in any particular order with respect to either their key or mapped values, instead organizes into buckets depending on their hash values to allow for fast access to individual elements directly by their key values.

Unordered map performs better than map while accessing individual elements by their keys. But for range iteration their performance is considerably low.

Definition

Below is definition of std::unordered_map from <unordered_map> header file

template < class Key,
           class T,
           class Hash = hash<Key>,
           class Pred = equal_to<Key>,
           class Alloc = allocator< pair<const Key,T> >
           > class unordered_map;

Parameters

  • Key − Type of the key.

  • T − Type of the mapped values.

  • Hash − A unary function object type which takes an object of type key type as argument and returns a unique value of type size_t based on it.

  • Pred − A binary predicate that which two arguments of the key type and returns a bool.

  • Alloc − Type of the allocator object.

  • T may be substituted by any other data type including user-defined type.

Member types

Following member types can be used as parameters or return type by member functions.

Sr.No. Member types Definition
1 key_type Key (First parameter of the template)
2 mapped_type T (Second parameter of the template)
3 value_type pair<const key_type,mapped_type>
4 hasher The third template parameter (defaults to: hash<key_type>)
5 key_equal The fourth template parameter (defaults to: equal_to<key_type>)
6 allocator_type Alloc (Fifth parameter of the template)
7 reference value_type&
8 const_reference const value_type&
9 pointer allocator_traits<Alloc>::pointer
10 const_pointer allocator_traits<Alloc>::const_pointer
11 iterator A forward iterator to value_type value_type
12 const_iterator A forward iterator to const value_type value_type
13 local_iterator A forward iterator to value_type
14 const_local_iterator A forward iterator to const value_type
15 difference_type ptrdiff_t
16 size_type size_t

Functions from <unordered_map>

Below is list of all methods from <unordered_map> header.

Constructors

Destructor

Member functions

Non-member overloaded functions

Introduction to unordered_multimap

Unordered_multimap is dictionary like data structure. It is a sequence of (key, value) pair, where different elements can have equivalent keys. Elements with equivalent keys are grouped together in the same bucket and in such a way that an equal_range iterator can iterate through all of them.

Unordered_multimap does not sort its element in any particular order with respect to either their key or mapped values, instead organizes into buckets depending on their hash values to allow for fast access to individual elements directly by their key values.

Definition

Below is definition of std::unordered_multimap from <unordered_map> header file

template < class Key, 
           class T,
           class Hash = hash<Key>,
           class Pred = equal_to<Key>,
           class Alloc = allocator< pair<const Key,T> >
           > class unordered_multimap;

Parameters

  • Key − Type of the key.

  • T − Type of the mapped values.

  • Hash − A unary function object type which takes an object of type key type as argument and returns a unique value of type size_t based on it.

  • Pred − A binary predicate that which two arguments of the key type and returns a bool.

  • Alloc − Type of the allocator object.

    T may be substituted by any other data type including user-defined type.

Member types

Following member types can be used as parameters or return type by member functions.

Sr.No. Member types Definition
1 key_type Key (First parameter of the template)
2 mapped_type T (Second parameter of the template)
3 value_type pair<const key_type,mapped_type>
4 hasher The third template parameter (defaults to: hash<key_type>)
5 key_equal The fourth template parameter (defaults to: equal_to<key_type>)
6 allocator_type Alloc (Fifth parameter of the template)
7 reference value_type&
8 const_reference const value_type&
9 pointer allocator_traits<Alloc>::pointer
10 const_pointer allocator_traits<Alloc>::const_pointer
11 iterator A forward iterator to value_type value_type
12 const_iterator A forward iterator to const value_type value_type
13 local_iterator A forward iterator to value_type
14 const_local_iterator A forward iterator to const value_type
15 difference_type ptrdiff_t
16 size_type size_t

Functions from <unordered_multimap>

Below is list of all methods from <unordered_map> header.

Constructors

Destructor

Member functions

Non-member overloaded functions