If you can use pointers to iterate through an array like this:
for (int *iter = arr; iter != std::end(arr); ++iter) {
// code
}
How do you iterate through a multidimensional array with pointers (without using auto
)?
EDIT: I am assuming this is an int[][]
such as {{3, 6, 8}, {2, 9, 3}, {4, 8, 2}}
If you declared array as arr[][], and yes you can because they are stored sequentially in memory. You can do:
for(int * iter = &arr[0][0]; iter != &arr[0][0] + col * row; iter++)
//...
How about trying like this:-
const int* d = data;
for ( int i = 0; i < width; ++i )
for ( int j = 0; j < height; ++j )
for ( int k = 0; k < depth; ++k )
sum += *d++;
Check this tutorial
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With