How to Use HashMap in C++? Last Updated : 28 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report A HashMap is a data structure in which the elements are stored in key-value pairs such that every key is mapped to a value using a hash function. In C++, hash maps are implemented using the unordered_map container class. In this article, we will learn how to use HashMap in C++. Example: Input:Key="Apple"; Value=10Key="Mango"; Value=20Key="Cherry"; Value=30Output:unordered_map<string, int> mp = {("Apple", 10), ("Mango", 20), ("Cherry", 30)}HashMaps in C++ In C++ STL there's a std::unordered_map that functions as a HashMap. This container stores key value pairs without any sorted order. It arranges them into buckets according to their hash values making it easier and faster to access elements using the keys directly. To use unordered_map we must include <unordered_map> header file in C++. #include <unordered_map>Syntax to Declare a HashMapunordered_map<key, value> hashMap;The average time complexity for search, insertion and deletion of a std::unordered_map is O(1), that is why it is very efficient. C++ Program for Demonstrating HashMap The below program demonstrates the usage of unordered_map as HashMap in C++. C++ // C++ program to use unordered_map as HashMap #include <iostream> #include <unordered_map> using namespace std; int main() { // Create an unordered_map unordered_map<string, int> umap; // Insert key-value pairs into the unordered_map umap["Apple"] = 10; umap["Mango"] = 20; umap["Cherry"] = 30; // Print the key-value pairs for (auto it : umap) cout << it.first << " " << it.second << endl; return 0; } OutputCherry 30 Mango 20 Apple 10 Time Complexity: O(1)Auxilliary Space: O(1) Comment More infoAdvertise with us Next Article How to Add Elements to a Map in C++? S shivanshmahajan876 Follow Improve Article Tags : C++ Programs C++ STL cpp-unordered_map CPP Examples misc-cpp +2 More Practice Tags : CPPSTL Similar Reads How to Store Vectors as Values in a Map? In C++, vectors are similar to arrays, but unlike arrays, the vectors are dynamic in nature. They can modify their size during the insertion or deletion of elements. On the other hand, Maps are containers that allow the users to store data in the form of key-value pairs. In this article, we will lea 2 min read How to Declare a Map in C++? In C++, maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values. In this article, we will learn how to declare a map in C++. How to declare a map in C++?In C++, you can declare a map u 2 min read How to Create a Dictionary in C++? A dictionary is a data structure in python that enables users to store data in the form of key-value pairs, where each key must be unique. In this article, we will learn how to create a dictionary-like data structure in C++.ExamplesInput: {{1 : "C++"}, {3: "Python"}, {2: "Java"}}Output: 1 : C++ 2 : 3 min read How to Add Elements to a Map in C++? In C++, a map is an associative container that stores the elements as key-value pairs where each element has a key and a mapped value. In this article, we will learn how to add elements to a map in C++ STL. For Example, Input: myMap = {{1, "Ram"}, {2, "Mohit"}}; Output: myMap = {{1, "Ram"}, {2, "Moh 2 min read How to Create a Stack of Map in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::map is an associative container that stores key-value pairs. In this article, we will learn how to create a stack of a map in C++. Example:Input: myMap = { {1: âaâ}, {2: âbâ}, {3: âcâ} }; myMap = { {4 2 min read How to Find the Size of a Map in C++? In C++ STL, maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values. In this article, we will learn how to find the size of a map in C++ STL. Example: Input: myMap = { {1, "Sravan"}, { 2 min read Like