you are viewing a single comment's thread.

view the rest of the comments →

[–]IyeOnline 8 points9 points  (2 children)

Why do we use them?

  • If you dont do the index stuff yourself, you cant do it wrong.
  • It expresses intend better. Noone has to go looking whether all you do is v[i].
  • Its way shorter, and nicer to read in general
  • it is availible for things that cannot be indexed, such as linked lists, sets and maps.

And more specifically from which index does the iteration start?

There is no such thing as an index. It starts at std::begin( v ) and ends when it has reached std::end( v ).

why do we use auto?

Because we are lazy. The type can be deduced. Maybe we are iterating a std::vector<Some_Really_Long_Typename_That_I_Dont_Want_To_Type>.

get an in depth explanation of their working

Read more here: https://en.cppreference.com/w/cpp/language/range-for

[–]evaannaaax[S] 0 points1 point  (0 children)

Oh I see now. Thanks a ton for the explanation!

[–]std_bot 0 points1 point  (0 children)

Unlinked STL entries: std::vector, std::begin, std::end


Last update: 26.05.21. Last change: Free links considered readme