Updated April 3, 2023
Introduction to C++ unordered_map
In C++, Unordered maps are considered associative containers, which helps store elements generated by the key-value and mapped value combination. This function permits the fast retrieval of separate elements that are based on their keys. Here, the key value is mainly used to find the item uniquely, and the mapped value is considered an object with the content linked to this key. There may be differences in the types of key-value and mapped value. Let us see more about the unordered map in the following sections. In this topic, we are going to learn about C++ unordered_map.
Definition
Below is the definition of unordered maps in C++
template< class K , class T,class Hash = hash<K>,
classPred = equal_to<K>,
classAlloc = allocator< pair<constK,T>>
classunordered_map;
The parameters are:
- K, which is the key type.
- T, which is then mapped value type
- Hash, a type of unary function object that gets an object of key type as a parameter and returns a specific value of size t.
- Pred, this is a binary predicate.
- Alloc, which is the type of object of the allocator.
T may be replaced by any data type containing a user-defined type.
Member Types of C++ unordered_map
Below are the member types that can be used by member functions as arguments or return types.
Member types | Description |
key_type | Key; Parameter 1 used for the template |
mapped_type | T; Parameter 2 used for the template |
hasher | Default value: hash<key_type>
;Parameter 3 used for the template |
key_equal | Default value: equal_to<key_type>);
Parameter 4 used for the template |
allocator_type | Alloc; Parameter 5 used for the template |
value_type | pair<constkey_type,mapped_type> |
reference | value_type& |
const_reference | constvalue_type& |
difference_type | ptrdiff_t |
size_type | size_t |
pointer | allocator_traits<Alloc>::pointer |
iterator | A forward iterator for the value_type value_type |
local_iterator | A forward iterator for the value_type |
const_iterator | A forward iterator for the constvalue_type value_type |
const_pointer | allocator_traits<Alloc>::const_pointer |
const_local_iterator | A forward iterator for the constvalue_type |
Constructors
The following are the constructors of the c++ unordered map.
- unordered_map::unordered_mapdefault constructor
An empty unordered_map will be constructed with a number of elements as zero.
- unordered_map::unordered_mapcopy constructor
An unordered_map will be constructed with each element’s copy. Thus, these elements will be already on the existing map.
- unordered_map::unordered_mapmove constructor
An unordered_map will be constructed with the content present in another map using the semantics move.
- unordered_map::unordered_maprange constructor
An unordered_map will be constructed with items in the range from first to last.
- unordered_map::unordered_mapinitializer_list constructor
An unordered_map will be constructed from the initializer list.
Methods on unordered_map
In an unordered map of C++, a plethora of functions is present. The most useful among them are = operator, [] operator, iterator begin and end, size and empty for capacity, lookup find and count, modification- insert and erase.
How unordered_map function work in C++?
In unordered_map, the elements are not sorted initially based on any particular order with respect to key values or mapped values. Instead, it is but structured into buckets subject to the hash values to permit fast access to distinct items directly by their values of keys.
Moreover, the containers of the unordered maps are faster than the containers of the map to access distinct elements based on their key, even though they are usually less efficient for iteration based on range through their elements subset.
These unordered maps implement the operator [], also known as a direct access operator that permits the mapped value direct access using its key value.
Let us understand more about the unordered map using the sample code.
- Define the unordered_map with some elements
unordered_map<char, int>mp = {
{'H', 21} ,
{'I', 52} ,
{'J', 36} ,
{'K', 47} ,
{'L', 54}
};
- Define an Iterator itr
auto itr = mp.find('L');
- print the values based on the requirement
Examples of C++ unordered_map
To understand more about the unordered map, let us work with some sample programs.
Example #1
C++ program to find a particular element in an unordered map.
Code:
#include <iostream>
#include <unordered_map>
//use the namespace as std
using namespace std;
//code for unordered_map begins here
int main(void)
{
//define the unordered_map
unordered_map<char, int>mp = {
{'H', 21} ,
{'I', 52} ,
{'J', 36} ,
{'K', 47} ,
{'L', 54}
};
//Iterator itr
auto itr = mp.find('L');
//print the iterator than links to the character 'L'
cout<< "Iterator links to " <<itr->first
<<" is " <<itr->second <<endl;
return 0;
}
Output:
First, use the namespace as std. Then, define the unordered_map with elements {‘H’, 21} ,{‘I’, 52} , {‘J’, 36} , {‘K’, 47} , {‘L’, 54. Once the elements are defined, use the iterator to find the element L and key that is linked to that particular character. On executing the code, the value linked to L will be printed.
Example #2
C++ program to print all the elements in an unordered map.
Code:
#include <iostream>
#include <iterator>
#include <unordered_map>
//use the namespace as std
using namespace std;
//code for unordered_map begins here
int main()
{
unordered_map<int, char> mp1;
unordered_map<int, char>::iterator crs;
mp1[1] = 'a';
mp1[2] = 'b';
mp1[3] = 'c';
mp1[4] = 'd';
mp1[5] = 'e';
mp1[6] = 'f';
mp1[7] = 'g';
mp1[8] = 'h';
cout<< "Key value\t corresponding element" <<endl;
for (crs = mp1.begin(); crs != mp1.end(); crs++)
{
cout<<crs->first;
cout<< '\t' <<crs->second << '\n'
<<endl;
}
}
Output:
In this program, also, first, use the namespace as std. Then, define the unordered_map with elements {and an iterator. Once the elements are defined, use the iterator to find all the elements and key that is linked to that particular character. It is done with the help of using begin() and end () functions. Finally, on executing the code, all the elements and corresponding values will be printed.
Conclusion
Unordered maps are the associative containers that help in storing elements generated by the key-value and mapped value combination. In this article, different aspects such as definition, constructors, methods, working, and examples of the unordered map are explained in detail.
Recommended Articles
This is a guide to C++ unordered_map. Here we discuss How the unordered_map function work in C++ and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –