consider:
int ia[3][4]; (auto row : ia) (auto col : row)
the first
for
iterates through ia, elements arrays of size 4. becauserow
not reference, when compiler initializesrow
convert each array element (like other object of array type) pointer array’s first element. result, in loop type ofrow
int*
.
i not sure understand how auto works, if can assume automatically gives type row based on ia
array members type, don't understand why kind of for
, row not reference, not valid. why going happen? "pointer array’s first element", because of what?
the problem row
int *
, not int[4]
1 expect because arrays decay pointers , there no automatic way know how many elements pointer points to.
to around problem std::array
has been added works expected:
#include <array> int main() { std::array<std::array<int, 4>, 3> ia; (auto &row : ia){ (auto &col : row){ col = 0; } } }
note &
before row
, col
indicate want reference , not copy of rows , columns, otherwise setting col
0 have no effect on ia
.
Comments
Post a Comment