How to iterate over the map in C++

In this quick tutorial, we will see how to iterate over in map in C++.

There are multiple ways to iterate over the map in C++. With newer versions of C++, there are more advanced ways to iterate over the map in C++.

Let’s go through each one by one.

1. Using for loop with stp::map
We have created a map named <code>countryCapitalMap</code> and inserted key-value pairs to it.

<pre>
#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>
using namespace std;
int main() {
    // Initialize a map
    map>"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 (auto itr = countryCapitalMap.begin(); itr != countryCapitalMap.end(); itr++)
    {
        std::cout << itr->first // Access key
              << ':'
              << itr->second // Access value
              << std::endl;
    }
    return 0;
}
</pre>
Output:
<pre>
China:Beijing
France:Paris
India:Delhi
Nepal:Kathmandu
</pre>

As you can see, we have printed country:capital(key:value) using for loop.

If you notice, we have used <code>auto</code> type specifier for map iterator because of readability. You can use <code>map<string, string>::iterator</code> explicitly as well.
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.

2. Using while loop with stp::map
We can also use a while loop instead of for loop.

<pre>
#include <iostream>
#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;
}
</pre>
Output:
<pre>
China:Beijing
France:Paris
India:Delhi
Nepal:Kathmandu
</pre>

3. 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.

<pre>
#include <iostream>
#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;
}
</pre>
Output:
China:Beijing
France:Paris
India:Delhi
Nepal:Kathmandu

4. 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.

<pre>
#include <iostream>
#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;
}
</pre>
Output:
China:Beijing
France:Paris
India:Delhi
Nepal:Kathmandu

That’s all about how to iterate over the map in C++.

Happy coding!



from Linux Hint https://ift.tt/2QLLhgq

Post a Comment

0 Comments