7.5. Range-Based for Statement


As we’ve shown, it’s common to process all the elements of an array. The new C++11 range-based for statement allows you to do this without using a counter, thus avoiding the possibility of “stepping outside” the array and eliminating the need for you to implement your own bounds checking.

Image Error-Prevention Tip 7.2

When processing all elements of an array, if you don’t need access to an array element’s subscript, use the range-based for statement.

The syntax of a range-based for statement is:

for ( rangeVariableDeclaration ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.