Other Common array Manipulations

Many common array manipulations use for statements. For example, the following for statement sets all the elements in row 2 of array a in Fig. 7.19 to zero:

for ( size_t column = 0; column < 4; ++column )   a[ 2 ][ column ] = 0;

The for statement varies only the second subscript (i.e., the column subscript). The preceding for statement is equivalent to the following assignment statements:

a[ 2 ][ 0 ] = 0;a[ 2 ][ 1 ] = 0;a[ 2 ][ 2 ] = 0;a[ 2 ][ 3 ] = 0;

The following nested counter-controlled for statement determines the total of all the elements in array a in Fig. 7.19:

total = 0;for ( size_t row = 0; row < a.size(); ++row )   for ( size_t column ...

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.