How would I use an enhanced for loop with a multidimensional array? (c++11, although feel free to answer for other versions)
We'll start with two dimensions...
int array[10][9];
//loop through first dimension (10)
for(int i : sizeof(array)) {
//do something
}
Compiler error: this range-based 'for' statement requires a suitable "begin" function and none was found
Could it have something to do with a multidimensional array being really still 1 dimension? In other words, int array[10][9] is equivalent to int array[90]
To use range based loops, you should pass a container in which the begin
and end
has beed defined. Try this:
for(auto &rows: array) // rows
{
for(auto &x: rows)
{
// ...
}
}
int array[10][9]
is not equivalent to int array[19]
, it can be defined as int array[10*9]
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.