3d & # 8594; 1D array indexing

in C ++, what is the indexing value for a W * H * D 3D array?

for a particular i, j, k this is the correct indexing:

i * W * H + J * W + K

+3
source share
5 answers

What you wrote is equivalent to pointer arithmetic that this would do:

T x[D][H][W];

x[i][j][k];  // Pointer arithmetic done here

It is obvious that, depending on how you order D, Hand W(or i, j, k), the calculation will be different.

+6
source

"" , , , . , , , . Fortran (), "" C.

0

, . , , .

0

, i 0... D-1, j 0... H-1 k 0... W-1.

, , , , , , ( ). , 3d- , . , x[i][j][k], .

. http://www.nr.com/cpppages/chapappsel.pdf .

0

,

for i
    for j
        for k

order. Thus, this would be the fastest, because the index of the array increases by one each time, and the values ​​can be unloaded. There is not one right way to do this, but you have probably chosen the best option.

-4
source

Source: https://habr.com/ru/post/1788045/