Using for loop with std::map
We have created a map named countryCapitalMap and inserted key-value pairs to it. We then use an iterator to do a for loop through the members of the map data structure.
#include <map>
#include <string>
#include <iterator>
using namespace std;
int main() {
// Initialize a map
map<string, string>countryCapitalMap;
countryCapitalMap.insert(pair<string, string>("Nepal", "Kathmandu"));
countryCapitalMap.insert(pair<string, string>("China", "Beijing"));
countryCapitalMap.insert(pair<string, string>("France", "Paris"));
// Iterate using iterator in for loop
for (auto itr = countryCapitalMap.begin();
itr != countryCapitalMap.end(); itr++) {
std::cout << itr->first // Access key
<< ':'
<< itr->second // Access value
<< std::endl;
}
return 0;
}
China:Beijing
France:Paris
Nepal:Kathmandu
As you can see, we have printed country:capital(key:value) using for loop.
If you notice, we have used auto type specifier for map iterator because of readability. You can use map
Note: If you see the output, it is sorted by keys in ascending order. This is because std::map is a sorted associative container with supplied Comparator(version C++11 onwards). Since we did not provide any comparator, so C++ has used the default Comparator for string.
Using while loop with stp::map
We can also use a while loop instead of for loop.
#include <map>
#include <string>
#include <iterator>
using namespace std;
int main() {
// Initialize a map
map<string, string> countryCapitalMap;
// Insert different elements in map
countryCapitalMap.insert(pair<string, string>("India", "Delhi"));
countryCapitalMap.insert(pair<string, string>("Nepal", "Kathmandu"));
countryCapitalMap.insert(pair<string, string>("China", "Beijing"));
countryCapitalMap.insert(pair<string, string>("France", "Paris"));
// Create a iterator for the map and Initialize with begin
auto itr=countryCapitalMap.begin();
// Iterate using iterator in while loop
while (itr!=countryCapitalMap.end())
{
std::cout << itr->first // Access key
<< ':'
<< itr->second // Access value
<< std::endl;
itr++;
}
return 0;
}
Output:
France:Paris
India:Delhi
Nepal:Kathmandu
Using Range based for loop (C++11 version onwards)
If you are using C++11 version, then this is most elegant way to iterate over map in C++. You can avoid traditional cubersome loops and use this instead.
#include <map>
#include <string>
using namespace std;
int main() {
// Initialize a map
map<string, string> countryCapitalMap;
// Insert different elements in map
countryCapitalMap.insert(pair<string, string>("India", "Delhi"));
countryCapitalMap.insert(pair<string, string>("Nepal", "Kathmandu"));
countryCapitalMap.insert(pair<string, string>("China", "Beijing"));
countryCapitalMap.insert(pair<string, string>("France", "Paris"));
// Iterate using iterator in for loop
for (const auto &ele : countryCapitalMap) {
cout <<ele.first << ":" << ele.second<<"\n";
}
return 0;
}
China:Beijing
France:Paris
India:Delhi
Nepal:Kathmandu
Using range-based for loop with key-values pairs(C++17 version onwards)
This version is supported from c++17 onwards and provides a more flexible way for iterating over the map. You can explicitly access key-values pair in map which provides even more readable solution.
#include <map>
#include <string>
using namespace std;
int main() {
// Initialize a map
map<string, string> countryCapitalMap;
// Insert different elements in map
countryCapitalMap.insert(pair<string, string>("India", "Delhi"));
countryCapitalMap.insert(pair<string, string>("Nepal", "Kathmandu"));
countryCapitalMap.insert(pair<string, string>("China", "Beijing"));
countryCapitalMap.insert(pair<string, string>("France", "Paris"));
// Iterate using iterator in for loop
for (const auto& [key, value] : countryCapitalMap) {
cout << key << ":" << value << "\n";
}
return 0;
}
China:Beijing
France:Paris
India:Delhi
Nepal:Kathmandu
That’s all about how to iterate over the map in C++.