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 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 ] << " ";

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.