...

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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.