Power Tools: auto, Lambdas & <algorithm>
10 min · C++
Skip repeating types with auto, write inline lambdas, and transform vectors with <algorithm>.
Enemy 1 of 5
auto and range-based for
Writing out full types like vector<string>::iterator gets verbose fast, so C++ gives you auto: put auto where a type would go, and the compiler figures out the actual type from the value you're assigning. It doesn't make C++ dynamically typed — the type is still fixed and checked at compile time, auto is purely a shorthand so you don't have to spell it out yourself.
A range-based for loop, written for (auto n : container), walks every element of a container (a vector, a map, an array) one at a time without you managing an index variable at all. It's the cleanest way to say "do this for each item" when you don't need to know the position of each item.
Loading editor...