Structured iteration

It is relatively easy to get your for-loops wrong. Luckily, C++ offers more and more bug-proof alternatives.

Structured iteration

by Andrzej Krzemieński

From the article:

These problems do not occur when you use the range-based for-loop.

This C++11 addition, apart from other conveniences, is a safety feature (as in language-safety): it is very hard to use it incorrectly. It is not flexible, or versatile. You have to pass it a range, you have to give a name to an element referenced in each iteration step. There is no "control variable" (like i), so you cannot get the operations on it wrong. A number of bugs are prevented simply by employing a range-based loop.

But what if my iteration is more complicated? What if I need to visit my elements in reverse?

 

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.