C++: Range-Based loops

I have come across a new way to loop through collections and range.

It consist of example code below:

for (auto const &x : vec)
{
  // x is a reference to an const item of vec
  // We can not change vec's items by changing x 
}

This is definitely new to me and a welcome change as using auto creates a placeholder for any types and can be anything with a proper cast to return back to its original form.

The new loop also simplifies the code as more explain by the link:

https://stackoverflow.com/questions/15176104/c11-range-based-loop-get-item-by-value-or-reference-to-const

Leave a Reply

Your email address will not be published. Required fields are marked *