Traversing a Map and unordered_map in C++ STL Last Updated : 11 Jan, 2025 Comments Improve Suggest changes Like Article Like Report The maps are described as mapped associative containers for elements where each element has a key and value assigned to it. Another form of map container seen in the C++ STL is the unordered map. It is the same as map containers just that they don't store the data in sorted order.We can traverse map and unordered_map using 4 different ways which are as follows:Using a ranged based for loopUsing begin() and end()Using IteratorsUsing std::for_each and lambda function1. Using a Range Based for LoopWe can use a range-based for loop to iterate over a map or an unordered_map in C++.Example: map // C++ program to traverse a map using range // based for loop #include <bits/stdc++.h> using namespace std; int main() { int arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 }; int n = sizeof(arr) / sizeof(arr[0]); // inserting elements map<int, int> m; for (int i = 0; i < n; i++) m[arr[i]]++; // Printing of MAP cout << "Element Frequency" << endl; for (auto i : m) cout << i.first << " \t\t\t " << i.second << endl; return 0; } unordered_map // C++ program to traverse a unordered_map using // range based for loop #include <bits/stdc++.h> using namespace std; int main() { int arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 }; int n = sizeof(arr) / sizeof(arr[0]); unordered_map<int, int> m; for (int i = 0; i < n; i++) m[arr[i]]++; // Printing of Unordered_MAP cout << "Element Frequency" << endl; for (auto i : m) cout << i.first << " " << i.second << endl; return 0; } OutputElement Frequency 1 4 2 1 3 2 4 1Note: The above output is for map only. For unordered_map, the output rows can be in any order.2. Traversing using begin() and end()The begin() and end() are the member functions of container classes that return the iterator to the first and the last key-value pair in a map or unordered_map respectively. We can use them to traverse over a map or an unordered_map.Example: map // C++ program to traverse a map using begin() and end() #include <bits/stdc++.h> using namespace std; int main() { int arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 }; int n = sizeof(arr) / sizeof(arr[0]); // inserting data to map map<int, int> m; for (int i = 0; i < n; i++) m[arr[i]]++; // Printing of MAP cout << "Element Frequency" << endl; for (auto i = m.begin(); i != m.end(); i++) cout << i->first << " \t\t\t" << i->second << endl; return 0; } unordered_map // C++ program to traverse a unordered_map // using begin() and end() #include <bits/stdc++.h> using namespace std; int main() { int arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 }; int n = sizeof(arr) / sizeof(arr[0]); unordered_map<int, int> m; for (int i = 0; i < n; i++) m[arr[i]]++; // Printing of Unordered_MAP cout << " Element Frequency" << endl; for (auto i = m.begin(); i != m.end(); i++) cout << i->first << " " << i->second << endl; return 0; } OutputElement Frequency 1 4 2 1 3 2 4 13. Iterating over a map by using an STL IteratorWe can create an iterator of std::map (or std::unordered_map), initialize it to the start of the map, and increment it till the end to traverse the map.Example: C++ // C++ program to traverse a map using Iterators() #include <algorithm> #include <iostream> #include <iterator> #include <map> #include <string> int main() { // Map created std::map<std::string, int> ExampleMap; // elements are inserted into map ExampleMap.insert( std::pair<std::string, int>("Sunday", 1)); ExampleMap.insert( std::pair<std::string, int>("Monday", 2)); ExampleMap.insert( std::pair<std::string, int>("Tuesday", 3)); ExampleMap.insert( std::pair<std::string, int>("Wednesday", 4)); ExampleMap.insert( std::pair<std::string, int>("Thursday", 5)); ExampleMap.insert( std::pair<std::string, int>("Friday", 6)); ExampleMap.insert( std::pair<std::string, int>("Saturday", 7)); // map iterator created // iterator pointing to start of map std::map<std::string, int>::iterator it = ExampleMap.begin(); // Iterating over the map using Iterator till map end. while (it != ExampleMap.end()) { // Accessing the key std::string word = it->first; // Accessing the value int count = it->second; std::cout << word << " :: " << count << std::endl; // iterator incremented to point next item it++; } return 0; } OutputFriday :: 6 Monday :: 2 Saturday :: 7 Sunday :: 1 Thursday :: 5 Tuesday :: 3 Wednesday :: 44. Iterating over a map by using std::for_each and lambda functionBy using the std::for_each algorithm and lambda functions we can easily iterate over all the elements of the map. Here the lambda function will be used as a call-back function and will receive each map entry.Example: map // C++ program to traverse a map using // std::for_each() and lambda() #include <algorithm> #include <iostream> #include <iterator> #include <map> #include <string> int main() { // Map created std::map<std::string, int> ExampleMap; // elements are inserted into map ExampleMap.insert( std::pair<std::string, int>("Sunday", 1)); ExampleMap.insert( std::pair<std::string, int>("Monday", 2)); ExampleMap.insert( std::pair<std::string, int>("Tuesday", 3)); ExampleMap.insert( std::pair<std::string, int>("Wednesday", 4)); ExampleMap.insert( std::pair<std::string, int>("Thursday", 5)); ExampleMap.insert( std::pair<std::string, int>("Friday", 6)); ExampleMap.insert( std::pair<std::string, int>("Saturday", 7)); // map iterator created // iterator pointing to start of map std::map<std::string, int>::iterator it = ExampleMap.begin(); // Iterating over the map till map end. std::for_each( ExampleMap.begin(), ExampleMap.end(), [](std::pair<std::string, int> p) { std::cout << p.first << " :: " << p.second << std::endl; }); return 0; } unordered_map // C++ program to traverse a unordered_map using // std::for_each() and lambda() #include <algorithm> #include <iostream> #include <iterator> #include <string> #include <unordered_map> int main() { // Map created std::unordered_map<std::string, int> ExampleMap; // elements are inserted into unordered_map ExampleMap.insert( std::pair<std::string, int>("Sunday", 1)); ExampleMap.insert( std::pair<std::string, int>("Monday", 2)); ExampleMap.insert( std::pair<std::string, int>("Tuesday", 3)); ExampleMap.insert( std::pair<std::string, int>("Wednesday", 4)); ExampleMap.insert( std::pair<std::string, int>("Thursday", 5)); ExampleMap.insert( std::pair<std::string, int>("Friday", 6)); ExampleMap.insert( std::pair<std::string, int>("Saturday", 7)); // unordered_map iterator created // iterator pointing to start of unordered_map std::unordered_map<std::string, int>::iterator it = ExampleMap.begin(); // Iterating over the unordered_map till unordered_map // end. std::for_each(ExampleMap.begin(), ExampleMap.end(), [](std::pair<std::string, int> p) { std::cout << p.first << " :: " << p.second << std::endl; }); return 0; } OutputFriday :: 6 Monday :: 2 Saturday :: 7 Sunday :: 1 Thursday :: 5 Tuesday :: 3 Wednesday :: 4 Comment More infoAdvertise with us Next Article How to use unordered_map efficiently in C++ K Kartik Improve Article Tags : Misc C++ STL cpp-unordered_map Practice Tags : CPPMiscSTL Similar Reads Unordered Map in C++ STL In C++, unordered_map is an unordered associative container that stores data in the form of unique key-value pairs. But unlike map, unordered map stores its elements using hashing. This provides average constant-time complexity O(1) for search, insert, and delete operations but the elements are not 7 min read Different Ways to Initialize an unordered_map in C++ Initialization is the process of assigning the initial values to the std::unordered_map elements. In this article, we will learn different methods to initialize the std::unordered_map in C++.Table of ContentUsing Initializer ListBy Inserting Elements One by OneFrom Another std::unordered_mapFrom Ano 3 min read Commonly Used Methodsunordered_map begin() in C++The unordered_map::begin() is a built-in function in C++ STL which returns an iterator pointing to the first element in the unordered_map container or in any of its bucket. Syntax for first element in unordered_map container: unordered_map.begin() Parameters: This function does not accepts any param 2 min read unordered_map end( ) function in C++ STLThe unordered_map::end() is a built-in function in C++ STL which returns an iterator pointing to the position past the last element in the container in the unordered_map container. In an unordered_map object, there is no guarantee that which specific element is considered its first element. But all 3 min read unordered_map size() in C++ STLThe unordered_multimap::size() is a built-in function in C++ Standard Template Library which return's the number of element in the unordered map. Syntax: unordered_multimap_name.size() Return Value: It returns the number of the element present in the unordered map. Time complexity: Constant i.e. O(1 1 min read unordered_map insert in C++ STLThe std::unordered_map::insert() in C++ STL is a built-in function used to insert a key-value pair in unordered_map container. As unordered maps only store unique elements, this function does not insert elements with duplicate keys. In this article, we will learn about std::unordered_map::insert() i 4 min read unordered_map at() in C++In C++, unordered_map at() is a library function used to find the value associated with a given key in the unordered_map container. Let's look at an example to see how to use this function.C++#include <bits/stdc++.h> using namespace std; int main() { unordered_map<int, string> um = {{1, 2 min read unordered_map find in C++ STLIn C++, std::unordered_map::find function is used to search for a specific element using the key in an unordered map container. It is a member function of std::unordered_map container defined inside <unordered_map> header file.Syntaxum.find(key);Parameterskey: The key of the element to be sear 1 min read unordered_map empty in C++ STLunordered_map::empty() function is used to check whether container size is zero or not. If container size is zero then it return TRUE otherwise it return FALSE. Syntax: unordered_map_name.empty() Parameters: This function does not accept any parameter Return type: This function returns boolean value 2 min read Other Member Methodsunordered_map max_size in C++ STLThe unordered_map::max_size is a built in function in C++ STL. It returns maximum number of elements which unordered_map can hold. Maximum number of elements in any container depends upon system and library implementation. Syntax size unordered_map.max_size() Parameters: It does not accept any param 2 min read unordered_map rehash in C++ STLThe std::unordered_map::rehash() is a built in function C++ STL which sets the number of buckets in container to n or more. Syntax void rehash( size_type s );If s is greater than the current buckets into the container then rehashed is done. The new bucket count can be greater than or equal to n.If s 2 min read bucket_count and bucket_size in unordered_map in C++Unordered_map is an associated container that stores elements formed by the combination of key-value and a mapped value. The key value is used to uniquely identify the element and the mapped value is the content associated with the key. Both key and value can be of any type predefined or user-define 4 min read unordered_map bucket() in C++ STLThe unordered_map::bucket() is a built-in STL function in C++ which returns the bucket number where the element with the key k is located in the map. Syntax: size_type bucket(key) Parameter: The function accepts one mandatory parameter key which specifies the key whose bucket number is to be returne 1 min read unordered_map load_factor in C++ STLThe unordered_map::load_factor() is a built-in function in C++ STL which returns the current load factor in the unordered_map container. The load factor is the ratio between the number of elements in the container (its size) and the number of buckets (bucket_count): load_factor = size / bucket_count 3 min read Traversing a Map and unordered_map in C++ STL The maps are described as mapped associative containers for elements where each element has a key and value assigned to it. Another form of map container seen in the C++ STL is the unordered map. It is the same as map containers just that they don't store the data in sorted order.We can traverse map 5 min read How to use unordered_map efficiently in C++ Pre-requisite: unordered_set,  unordered_map C++ provides std::unordered_set and std::unordered_map to be used as a hash set and hash map respectively. They perform insertion/deletion/access in constant average time. However, the worst-case complexity is O(n2).The reason is that the unordered_map 6 min read map vs unordered_map in C++ In C++, map and unordered_map are the containers that store can store data in the form of key-value pairs, but they differ significantly in terms of underlying implementation and performance characteristics.The below table lists the primary differences between map and unordered_map container:mapunor 2 min read How to create an unordered_map of tuples in C++? Tuple - A tuple is an object that can hold a number of elements. The elements can be of different data types. The elements of tuples are initialized as arguments in the order in which they will be accessed. Unordered Map does not contain a hash function for a tuple. So if we want to hash a tuple the 2 min read How to create an unordered_map of user defined class in C++? unordered_map is used to implement hash tables. It stores key value pairs. For every key, a hash function is computed and value is stored at that hash entry. Hash functions for standard data types (int, char, string, ..) are predefined. How to use our own data types for implementing hash tables?unor 3 min read Like