The C++ Standard Library
- C++ Library - <valarray>
- C++ Library - <utility>
- C++ Library - <typeinfo>
- C++ Library - <tuple>
- C++ Library - <thread>
- C++ Library - <string>
- C++ Library - <stdexcept>
- C++ Library - <regex>
- C++ Library - <numeric>
- C++ Library - <new>
- C++ Library - <memory>
- C++ Library - <locale>
- C++ Library - <limits>
- C++ Library - <functional>
- C++ Library - <exception>
- C++ Library - <complex>
- C++ Library - <atomic>
- C++ Library - <streambuf>
- C++ Library - <sstream>
- C++ Library - <ostream>
- C++ Library - <istream>
- C++ Library - <iostream>
- C++ Library - <iosfwd>
- C++ Library - <ios>
- C++ Library - <iomanip>
- C++ Library - <fstream>
- C++ Library - Home
The C++ STL Library
- C++ Library - <iterator>
- C++ Library - <algorithm>
- C++ Library - <vector>
- C++ Library - <unordered_set>
- C++ Library - <unordered_map>
- C++ Library - <stack>
- C++ Library - <set>
- C++ Library - <queue>
- C++ Library - <map>
- C++ Library - <list>
- C++ Library - <forward_list>
- C++ Library - <deque>
- C++ Library - <bitset>
- C++ Library - <array>
C++ Programming Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
C++ Library - <unordered_map>
Introduction to unordered_map
Unordered map is dictionary pke 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 inspanidual 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 inspanidual elements directly by their key values.
Unordered map performs better than map while accessing inspanidual 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 pst of all methods from <unordered_map> header.
Constructors
Sr.No. | Method & Description |
---|---|
1 | Constructs an empty unordered_map with zero elements. |
default constructor
2 | Constructs an unordered_map with copy of each elements present in existing unordered_map. |
copy constructor
3 | Constructs an unordered_map with the contents of other using move semantics. |
move constructor
4 | Constructs an unordered_map with as many elements as in range of first to last. |
range constructor
5 | Constructs an unordered_map from initiapze pst. |
initiapzer_pst constructor
Destructor
Sr.No. | Method & Description |
---|---|
1 | Destroys unordered_map object by deallocating it s memory. |
Member functions
Sr.No. | Method & Description |
---|---|
1 | Returns a reference to the mapped value associated with key k. |
2 | Returns an iterator which refers to the first element of the map. |
container iterator
3 | Returns an iterator pointing to the first element in one of its buckets. |
bucket iterator
4 | Returns the bucket number where element with key k is located. |
5 | Returns the number of buckets in unordered_map container. |
6 | Returns the number of elements presents in the nth bucket. |
7 | Returns a constant iterator which refers to the first element of the unordered_map. |
container iterator
8 | Returns a constant iterator pointing to the first element in one of its buckets. |
bucket iterator
9 | Returns a constant iterator which points to past-the-end element of the unordered_map. |
container iterator
10 | Returns a constant iterator which points to past-the-end element in one of its buckets. |
bucket iterator
11 | Destroys the unordered_map by removing all elements and sets the size of unordered_map to zero. |
12 | Returns the number of mapped values associated with key k. |
13 | Extends container by inserting new element. |
14 | Inserts a new element in unordered_map using hint as a position for element. |
15 | Tests whether unordered_map is empty or not. |
16 | Returns an iterator which points to past-the-end element in the unordered_map. |
container iterator
17 | Returns an iterator which points to past-the-end element in one of its buckets. |
bucket iterator
18 | Returns range of elements that matches specific key. |
19 | Removes single element of the unordered_map from position. |
position version
20 | Removes mapped value associated with key k. |
key version
21 | Removes range of element from the the unordered_map. |
range version
22 | Finds an element associated with key k. |
23 | Returns an allocator associated with unordered_map. |
24 | Calculates the hash function object used by the unordered_map container. |
25 | Extends container by inserting new element in unordered_map. |
26 | Extends container by inserting new element in unordered_map. |
move version
27 | Extends conta iner by inserting new element in unordered_map. |
hint version
28 | Extends unordered_map by inserting new element. |
move and hint version
29 | Extends container by inserting new elements in the unordered_map. |
range version
30 | Extends map by inserting new element from initiapzer pst. |
initiapzer_pst version
31 | Returns the function that compares keys for equapty. |
32 | Returns the current load factor of the unordered_map container. |
33 | Returns the maximum number of buckets that the unordered_map container can have. |
34 | Returns the current maximum load factor for the unordered_map container. |
get version
35 | Assigns new load factor for the unordered_map container. |
set version
36 | Returns the maximum number of elements can be held by unordered_map. |
37 | Assigns new contents to the unordered_map by replacing old ones and modifies size if necessary. |
copy version
38 | Move the contents of one unordered_map into another and modifies size if necessary. |
move version
39 | Copy elements from initiapzer pst to unordered_map. |
initiapzer_pst version
40 | If key k matches an element in the container, then method returns a reference to the element. |
41 | If key k matches an element in the container, then method returns a reference to the element. |
move version
42 | Sets the number of buckets in the container to n or more. |
43 | Sets the number of buckets in the container to the most appropriate to contain at least n elements. |
44 | Returns the number of elements present in the unordered_map. |
45 | Exchanges the content of first unordered_map with another. |
Non-member overloaded functions
Sr.No. | Method & Description |
---|---|
1 | Tests whether two unordered_maps are equal or not. |
2 | Tests whether two unordered_maps are equal or not. |
3 | Exchanges the content of first unordered_map with another. |
Introduction to unordered_multimap
Unordered_multimap is dictionary pke 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 inspanidual 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 pst of all methods from <unordered_map> header.
Constructors
Sr.No. | Method & Description |
---|---|
1 | Constructs an empty unordered_multimap with zero elements. |
default constructor
2 | Constructs an unordered_multimap with copy of each elements present in existing unordered_multimap. |
copy constructor
3 | Constructs an unordered_multimap with the contents of other using move semantics. |
move constructor
4 | Constructs an unordered_multimap with as many elements as in range of first to last. |
range constructor
5 | Constructs an unordered_multimap from initiapze pst. |
initiapzer_pst constructor
Destructor
Sr.No. | Method & Description |
---|---|
1 | Destroys unordered_multimap object by deallocating it s memory. |
Member functions
Sr.No. | Method & Description |
---|---|
1 | Returns an iterator which refers to the first element of the unordered_muptmap. |
container iterator
2 | Returns an iterator pointing to the first element in one of its buckets. |
bucket iterator
3 | Returns the bucket number where element with key k is located. |
4 | Returns the number of buckets present in unordered_multimap container. |
5 | Returns the number of elements presents in the nth bucket. |
6 | Returns a constant iterator which refers to the first element of the unordered_multimap. |
container iterator
7 | Returns a constant iterator pointing to the first element in one of its buckets. |
bucket iterator
8 | Returns a constant iterator which points to past-the-end element of the unordered_multimap. |
container iterator
9 | Returns a constant iterator which points to past-the-end element in one of its buckets. |
bucket iterator
10 | Destroys the unordered_multimap by removing all elements and sets the size of unordered_multimap to zero. |
11 | Returns the number of mapped values associated with key k. |
12 | Extends container by inserting new element. |
13 | Inserts a new element in a unordered_multimap using hint as a position for element. |
14 | Tests whether unordered_multimap is empty or not. |
15 | Returns an iterator which points to past-the-end element in the unordered_multimap. |
container iterator
16 | Returns an iterator which points to past-the-end element in one of its buckets. |
bucket iterator
17 | Returns range of elements that matches specific key. |
18 | Removes single element of the unordered_multimap from position. |
position version
19 | Removes mapped value associated with key k. |
key version
20 | Removes range of element from the the unordered_multimap. |
range version
21 | Finds an element associated with key k. |
22 | Returns an allocator associated with unordered_multimap. |
23 | Calculates the hash function object used by the unordered_multimap container. |
24 | Extends container by inserting new element in unordered_multimap. |
value version
25 | Extends unordered_multimap by inserting new element. |
move version
26 | Extends container by inserting new element in unordered_multimap. |
hint version
27 | Extends container by inserting new element in unordered_multimap by using move semantics. |
hint move version
28 | Extends container by inserting new elements in the unordered_multimap. |
range version
29 | Extends unordered_multimap by inserting new element from initiapzer pst. |
initiapzer_pst version
30 | Returns the function that compares keys for equapty. |
31 | Returns the current load factor of the unordered_multimap container. |
32 | Returns the maximum number of buckets that the unordered_multimap container can have. |
33 | Returns the current maximum load factor for the unordered_multimap container. |
get version
34 | Assigns new load factor for the unordered_multimap container. |
set version
35 | Returns the maximum number of elements can be held by unordered_multimap. |
36 | Assigns new contents to the unordered_multimap by replacing old ones and modifies size if necessary. |
copy version
37 | Move the contents of one unordered_multimap into another and modifies size if necessary. |
move version
38 | Copy elements from initiapzer pst to unordered_multimap. |
initiapzer_pst version
39 | Sets the number of buckets in the container to n or more. |
40 | Sets the number of buckets in the container to the most appropriate to contain at least n elements. |
41 | Returns the number of elements present in the unordered_multimap. |
42 | Exchanges the content of first unordered_multimap with another. |
Non-member overloaded functions
Sr.No. | Method & Description |
---|---|
1 | Tests whether two unordered_multimaps are equal or not. |
2 | Tests whether two unordered_multimaps are equal or not. |
3 | Exchanges the content of first unordered_multimap with another. |