You are reading the article How Unordered_Map Function Work In C++? updated in October 2023 on the website Vibergotobrazil.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How Unordered_Map Function Work In C++?
Introduction to C++ unordered_mapIn 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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Definitionclassunordered_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_mapBelow 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
;Parameter 3 used for the template
key_equal
Parameter 4 used for the template
allocator_type Alloc; Parameter 5 used for the template
value_type
reference value_type&
const_reference constvalue_type&
difference_type ptrdiff_t
size_type size_t
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
const_local_iterator A forward iterator for the constvalue_type
ConstructorsThe 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 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
{‘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_mapTo understand more about the unordered map, let us work with some sample programs.
Example #1C++ program to find a particular element in an unordered map.
Code:
//use the namespace as std using namespace std; int main(void) { {‘H’, 21} , {‘I’, 52} , {‘J’, 36} , {‘K’, 47} , {‘L’, 54} }; auto itr = mp.find(‘L’); 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 #2C++ program to print all the elements in an unordered map.
Code:
//use the namespace as std using namespace std; int main() { 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 valuet corresponding element” <<endl; for (crs = mp1.begin(); crs != mp1.end(); crs++) { <<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.
ConclusionUnordered 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 ArticlesThis 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 –
You're reading How Unordered_Map Function Work In C++?
Update the detailed information about How Unordered_Map Function Work In C++? on the Vibergotobrazil.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!