In this article, we will explore different methods to loop through a map in C++, and when to use them.
What is map in C++
An associative container known as a map has a pair of keys and values for each element. Two elements having the same key are not possible. Regardless of whether an element represents an int or a string, the map arranges the elements by using an individual comparator function in ascending order. It saves the values in a mapped format. It’s included in a map header file.
Syntax for map
The following is the syntax for the map in C++.
Here, datatype refers to the data type of both the key and the value of the map.
For example, if you want to create a map with string keys and integer values, the syntax would be:
How to Loop Through a map in C++?
We can loop through the map in three ways, which are as follows:
Now we will understand these methods one by one.
1: Using STL Iterators
To iterate over all the elements in a map in C++, you can create an iterator of the type map and initialize it with the map’s beginning. You can then loop through the map using this iterator until it reaches the end. During each iteration, you can access the key and its corresponding value using the iterator. The iterator is incremented in order to direct to the following element in the map after the current element has been accessed.
#include <map>
using namespace std;
int main() {
map<string, int> myMap;
myMap["apple"] = 10;
myMap["banana"] = 20;
myMap["orange"] = 30;
// Iterate over the map
for(auto i = myMap.begin(); i != myMap.end(); ++i) {
cout << "Key: " << i->first << " Value: " << i->second << endl;
}
return 0;
}
2: Using Range-Based for Loop
You can also use a range-based for loop to iterate over a map in C++. Here is an example:
#include <map>
using namespace std;
int main() {
map<string, int> myMap = {{"apple", 10}, {"banana", 20}, {"cherry", 30}};
for (const auto& pair : myMap) {
cout << "Key: " << pair.first << ", Value: " << pair.second << endl;
}
return 0;
}
3: Using for_each and Lambda Function
The for_each method and a lambda function can be used to traverse a map in C++. The lambda function acts as the callback and is used to call for every element in the map. Here is an example of how it can be done:
#include <map>
#include <algorithm>
using namespace std;
int main() {
map<string, int> my_map = {{"apple", 10}, {"banana", 20}, {"cherry", 30}};
// use for_each and lambda function to traverse the map
for_each(my_map.begin(), my_map.end(), [](auto const &pair) {
cout << pair.first << " has a value of " << pair.second << endl;
});
return 0;
}
Conclusion
Looping through a map in C++ is a fundamental skill that any programmer working with maps should know. We have explored three different methods to loop through a map, including using STL iterators, a range-based for loop, for_each, and lambda functions. With the knowledge gained from this article, you should be able to choose the best method for your particular use case and efficiently iterate through a map in C++.