The range-based for
statement simplifies the code for iterating through an array
. Line 13 can be read as “for each iteration, assign the next element of items
to int
variable item
, then execute the following statement.” Thus, for each iteration, identifier item
represents one element in items
. Lines 13–14 are equivalent to the following counter-controlled repetition:
for ( int counter = 0; counter < items.size(); ++counter )
cout << items[ counter ] << " ";