Summary: In this tutorial, we will learn different ways to iterate through a map in C++ programming language.

Method 1: Using C++11 Range-Based for Loop

The simplest way to iterate through a map is to use the range-based for loop (introduced in C++11) along with the auto keyword.

#include <iostream>
#include <map>
using namespace std;

int main() {
    map<string, string> moviesRating = {{"Avengers", "9/10"},
                                        {"Justice League", "8/10"},
                                        {"Inception", "10/10"}};
    
    //iterate using auto
    for(auto m: moviesRating){
        // m.first is the key and m.second is the value 
        cout << m.first << ": " << m.second << endl;
    }

    return 0;
}

Output:

Avengers: 9/10
Inception: 10/10
Justice League: 8/10

The auto keyword directs the compiler to deduce the type of the variable which makes the code more robust and simple.

Method 2: Using C++17 Range-Based for Loop

The update to range-based for loop in C++17 provides more flexibility to programmers for iterating over map in C++.

We can define the key and value parameters in the for loop declaration to easily access them inside the body of the loop.

#include <iostream>
#include <map>
using namespace std;

int main() {
    map<int, string> students = {{1, "Newton"},
                                 {2, "Simson"},
                                 {3, "Binod"}};
    
    //iterate over key-value pairs using auto
    for(auto [key, val]: students){
        cout << key << ": " << val << endl;
    }

    return 0;
}

Output:

1: Newton
2: Simson
3: Binod

Method 3: Using Traditional for Loop

In this method, we use the traditional for loop along with the iterator of type auto to iterate through the elements of the map.

#include <iostream>
#include <map>
using namespace std;

int main() {
    map<string, string> carSpeed = {{"NANO", "120mph"},
                                    {"Lamborghini", "330mph"},
                                    {"THAR", "250mph"}};
    
    //set start and end point, & iterate using the for loop
    for(auto i=carSpeed.begin(); i!=carSpeed.end(); i++){
        cout << i->first << ": " << i->second << endl;
    }

    return 0;
}

Output:

Lamborghini: 330mph
NANO: 120mph
THAR: 250mph

The auto keyword automatically deduces the type as map<string, string>::iterator when we initialize the variable i in the for loop statement.

Method 4: Using while Loop

We can rewrite the logic used in the previous method by using while loop instead of the for loop.

#include <iostream>
#include <map>
using namespace std;

int main() {
    map<int, int> squares = {{2, 4},
                             {3, 9},
                             {4, 16}};
    
    //point the iterator to the beginning of the map
    auto i=squares.begin();
    
    //loop until 'i' reaches the end
    while(i!=squares.end()){
        cout << i->first << ": " << i->second << endl;
        i++;
    }

    return 0;
}

Output:

2: 4
3: 9
4: 16

These are the 4 methods using which we can iterate though the key-value pairs of the map in C++.

Leave a Reply

2 × two =