How to loop through elements in unordered map in C++
std::unordered_map is a hash map implementation provided by the standard template library of C++. It is sometimes necessary to loop through elements of the map. So, in this article, we shall learn about the different ways to access elements in an unordered_map.
Using iterators:
The unordered_map provided a forward iterator. It can be used to loop only in the forward iterators. So, it’s only possible to loop only in the forward direction, and in most cases, it is more than enough. All three loops types (for, while, and do-while) can be used. However, for loop is always preferred for iteration. The unordered map provides begin() and end() member functions which can be used to get iterators to the first element and the one after the last.
The following code illustrates the use of for loop for iterating over an unordered map:
#include <unordered_map> #include <iostream> int main() { std::unordered_map<char, int> count; std::string g{}; std::getline(std::cin, g,'*'); for (int i = 0; i < g.length(); i++) // iterating through all charcters in string { char x = g[i]; if (isalpha(x)) count[x]++; //increase the value if its a alphabet } std::cout << "The count of each letter is\n"; for (auto i = count.begin(); i != count.end(); i++) //auto can be used to avoid verbose { std::cout << i->first << ":" << i->second << "\n"; } }
Input: All the world’s a stage, And all the men and women merely players; They have their exits and their entrances; And one man in his time plays many parts, His acts being seven ages.*
Output: The count of each letter is A:3 t:10 l:8 h:7 e:21 o:3 w:2 r:7 d:5 s:12 a:14 g:3 n:14 m:6 y:5 p:3 T:1 v:2 i:8 x:1 c:2 H:1 b:1
Using range-based for loop:
Range-based for loop can be used when only the elements of the map are accessed and modified. So, it’s always preferable to use a range-based for loop when direct accessing of iterators is not necessary. The general idea about range-based for loop can be found: here.
An unordered_map contains elements of type std::pair<datatype of key, datatype of mapped value >. The data type can be expressed using auto keyword to avoid verbosity and the elements can be accessed in front of the pair. But, this is considered an old-school way, and a better way to implement this is to use structured binding.
The following code illustrates the use of rage based for loop
#include <iostream> #include <unordered_map> int main() { std::unordered_map<char, int> count; std::string g{}; std::getline(std::cin, g, '*'); for (int i = 0; i < g.length(); i++) // iterating through all charcters in string { char x = g[i]; if (isalpha(x)) count[x]++; //increase the value if its a alphabet } std::cout << "The count of each letter is\n"; for (auto &[first,second]: count) //using structured binding { std::cout << first << ":" << second << "\n"; // values can also be manipulated as they are refrences } }
Output is the same as the normal for loop code using an iterator.
Leave a Reply