This is a very efficient way to store arrays, and it is right.
If you use a 3D array A[x][y][z] should be replaced by A[ x * height * depth + y * depth + z ]
If you have the 1D array A[index] and you want to see what that corresponds to in 3D,
1 2 3
width_index=index/(height*depth); //Note the integer division . This is x
height_index=(index-width_index*height*depth)/depth; //This is y
depth_index=index-width_index*height*depth- height_index*depth;//This is z
BEDMAS.
In this case you only have multiplications and additions. Do the multiplications first moving left to right
((x * Height) * Depth) + (y * Depth) + z
then additions (left to right as well)
((((x * Height) * Depth) + (y * Depth)) + z)
frankly I think that makes it much harder to read :P
It looks like it's harder to read, because it is... however, it makes a better "at a glance" impression, making the overall readability far higher despite the overuse of parenthesis.
As long as you treat the dimensions consistently, it doesn't really matter which one is named x, y or z. Encapsulating access to the data structure as htirwin suggests is one way to ensure the dimensions are treated consistently.
If you use a 3D array A[x][y][z] should be replaced by A[ x * height * depth + y * depth + z ]
This is false, C++'s implementation of 3D array is exactly the same as taking a 1D array and handling the index yourself. Though you do have mroe control, such as which way to store it (row vs col major). C++'s implementation just uses row major if i recall correctly. You might be confused with std::vector<> and how people tend to use that to create a N dimension array std::vector<std::vector<std::vector<int>>>. In this case a lot of memory is wasted storing the data of the vectors, which usually just corresponds some data such as pointers and such.
You can just use boost's implementation, multi_array.
Yes it is... http://ideone.com/559Cyj
What difference do you think it makes having it on the stack or having it allocated else where in memory, it makes no difference how C++ handles arrays.
Ah i get what you mean now. Still better to create a class to handle that, boost::multi_array should handle that. Your wording man, so much communication.