To process the elements of a two-dimensional array
, we use a nested loop in which the outer loop iterates through the rows and the inner loop iterates through the columns of a given row. Function printArray
’s nested loop is implemented with range-based for
statements. Lines 27 and 30 introduce the C++11 auto keyword, which tells the compiler to infer (determine) a variable’s data type based on the variable’s initializer value. The outer loop’s range variable row
is initialized with an element from the parameter a
. Looking at the array
’s declaration, you can see that the array
contains elements of type
array< int, columns >
so the compiler infers that row
refers to a three-element array
of int
values (again, columns
is 3). The const &
in row
’s declaration indicates that the reference cannot be used to modify the rows and prevents each row from being copied into the range variable. The inner loop’s range variable element
is initialized with one element of the array
represented by row
, so the compiler infers that element
refers to an int
because each row contains three int
values. In an IDE, you can typically hover your mouse over a variable declared with auto
and the IDE will display the variable’s inferred type. Line 31 displays the value from a given row and column.