My goal is to use range for loops to print out a two-dimensional array. My problem is I don't know how to specify the type of the loop control variable for the outer loop. If I use the auto keyword as shown below, all works fine.
#include <iostream>
usingnamespace std;
int main()
{
int ia[3][4] =
{
{0,1,2,3},
{4,5,6,7},
{8,9,10,11}
};
for( auto &row : ia )
{
for( int col : row )
{
cout << col << " ";
}
cout << "\n";
}
return 0;
}
problem is I can't use auto for this exercise, I have to specify the type directly, and the book I have doesn't provide an example of this. I've tried several different possibilities in place of auto, without success. If someone could provide an example, or point me in the right direction, I would be grateful.
#include <iostream>
#include <iomanip>
int main()
{
int ia[3][4] =
{
{0,1,2,3},
{4,5,6,7},
{8,9,10,11}
};
using row_type = int[4] ; // the type of a row is 'array of 4 int'
typedefint row_type[4] ; // same as above; alternative (old) syntax
for( const row_type& row : ia )
{
for( int v : row ) std::cout << std::setw(3) << v ;
std::cout << '\n' ;
}
}