...

items before modification: 1 2 3 4 5
items after modification: 2 4 6 8 10

Using the Range-Based for to Display an array’s Contents

The range-based for statement simplifies the code for iterating through an array. Line 12 can be read as “for each iteration, assign the next element of items to int variable item, then execute the loop’s body.” Thus, for each iteration, item represents one element value (but not a subscript) in items. In the range-based for’s header, you declare a so-called range variable to the left of the colon (:) and specify the name of an array to the right. You can use the range-based for statement with most of the C++ Standard Library’s prebuilt data structures (commonly called containers). Lines 12–14 are equivalent ...

Get C++ How to Program, 10/e now with O’Reilly online learning.

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