How to iterate over a C++ STL map data structure using the 'auto' keyword?

95,504

Solution 1

This code uses 2 new features from C++11 standard the auto keyword, for type inference, and the range based for loop.

Using just auto this can be written as (thanks Ben)

for (auto it=mymap.begin(); it!=mymap.end(); ++it)

Using just range for this can be written as

for (std::pair<const char,int>& x: mymap) {
        std::cout << x.first << " => " << x.second << '\n';
}  

Both of these do the exact same task as your two versions.

Solution 2

In addition to the previous answers, C++17 added another approach using structured bindings:

for (auto& [key, value]: mymap) {
        std::cout << key << " => " << value << '\n';
} 

Solution 3

The following worked for me:

for (auto x: mymap) {
  cout << x.first << endl;
}

Solution 4

I am curious to know the exact implications of using the keyword "auto" here.

It enables:

  • Less typing for a typical iterating code
  • Less chances of manual errors because compiler deduces the exact type of the iterator.

Solution 5

It's new feature of C++11, it's called Range-Based for Loops, which iterates over all elements of a given range, array, or collection. It’s what in other programming languages would be called a foreach loop The general syntax is as follows:

for ( decl : coll ) {
    statement
}

Auto: Automatic Type Deduction with auto

With C++11, you can declare a variable or an object without specifying its specific type by using, for example:

auto i = 42; // i has type int
double f();
auto d = f(); // d has type double
Share:
95,504

Related videos on Youtube

KT100
Author by

KT100

Updated on September 06, 2021

Comments

  • KT100
    KT100 over 2 years

    So far I have always used an iterator for traversing through all the keys in an STL map as follows:

        for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it){
                std::cout << it->first << "  => " << it->second << '\n';
        }
    

    Very recently though I came across some code that used a different style to iterate through the keys as shown below. Has this feature been added only recently in revised standard? It seems like a rather interesting way of getting more done with lesser code, as many other languages already provide.

        for (auto& x: mymap) {
                std::cout << x.first << " => " << x.second << '\n';
        }  
    

    Also, I am curious to know the exact implications of using the keyword "auto" here.

  • Ben Voigt
    Ben Voigt over 11 years
    And the intermediate version would be for (auto it=mymap.begin(); it!=mymap.end(); ++it)
  • KT100
    KT100 over 11 years
    What's the benefit of using 'auto' here?
  • Karthik T
    Karthik T over 11 years
    You dont need to type as much. With less boilerplate, the code should become more readable.
  • Karthik T
    Karthik T over 11 years
    @KT100 basically at the statement auto it=mymap.begin() the compiler will automatically infer that it is the iterator type for mymap and set the appropriate type, without you needing to figure it out and type it.
  • JoergB
    JoergB over 11 years
    The "just range" loop won't work as posted, as map<char, int>::value_type is pair<const char, int>.
  • Karthik T
    Karthik T over 11 years
    @KT100 This was a good example of the advantages of auto :P I wouldnt have messed it up.
  • Mooing Duck
    Mooing Duck about 8 years
    This makes copies of all of the elements, and doesn't actually answer the questions that the poster asked.
  • Tolli
    Tolli about 8 years
    You can find more information and examples of range-based for loops here: en.cppreference.com/w/cpp/language/range-for
  • Tolli
    Tolli about 8 years
    herbsutter.com/2013/08/12/… This explains why you should (almost) always use auto.