How to iterate over a C++ STL map data structure using the 'auto' keyword?
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
Related videos on Youtube
KT100
Updated on September 06, 2021Comments
-
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 over 11 yearsAnd the intermediate version would be
for (auto it=mymap.begin(); it!=mymap.end(); ++it)
-
KT100 over 11 yearsWhat's the benefit of using 'auto' here?
-
Karthik T over 11 yearsYou dont need to type as much. With less boilerplate, the code should become more readable.
-
Karthik T over 11 years@KT100 basically at the statement
auto it=mymap.begin()
the compiler will automatically infer thatit
is the iterator type formymap
and set the appropriate type, without you needing to figure it out and type it. -
JoergB over 11 yearsThe "just range" loop won't work as posted, as
map<char, int>::value_type
ispair<const char, int>
. -
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 about 8 yearsThis makes copies of all of the elements, and doesn't actually answer the questions that the poster asked.
-
Tolli about 8 yearsYou can find more information and examples of range-based for loops here: en.cppreference.com/w/cpp/language/range-for
-
Tolli about 8 yearsherbsutter.com/2013/08/12/… This explains why you should (almost) always use auto.