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

Lines 17–18 use a range-based for statement to multiply each element of items by 2. In line 17, the rangeVariableDeclaration indicates that itemRef is an int reference (&). Recall that a reference is an alias for another variable in memory—in this case, one of the array’s elements. We use an int reference because items contains int values and we want to modify each element’s value—because itemRef is declared as a reference, any change you make to itemRef changes the corresponding element value in the array.

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.