[英]C++ Multidimensional array
I have a 3D array 我有一个3D阵列
double values[30][30][30];
I have a loop where I assign values to the array; 我有一个循环,在其中给数组赋值; Something like:
就像是:
for(int z = 0;z<30; z++)
for (int y = 0;y<30; y++)
for (int x = 0;x<30; x++)
values[z][y][x] = intensity;
end
So this is how I am filling the array. 所以这就是我填充数组的方式。 The problem is that I want to create column in addition to intensity to store another variable.
问题是,除了强度之外,我还想创建一列以存储另一个变量。 For instance, the second to last line should be something like
例如,倒数第二行应类似于
values[z][y][x] = intensity | distance;
I hope you get the idea. 希望您能明白。 My knowledge is limited and I couldn't come up with a solution.
我的知识有限,我无法解决。 Thanks for your suggestions.
感谢您的建议。
This is really dependant on your datatypes. 这实际上取决于您的数据类型。 The easiest solution is using a struct:
最简单的解决方案是使用结构:
struct data {
float intensity; // or replace 'float' with whatever datatype you need
float distance;
};
Use this struct instead of the datatype you're using now for the array, then later on set the values: 使用此结构而不是您现在用于数组的数据类型,然后再设置值:
values[z][y][x].intensity = intensity;
values[z][y][x].distance = distance;
If you're using small values only (eg char for each value only) you could as well use bitwise operators to store everything in an integer: 如果仅使用较小的值(例如,仅将char用于每个值),则还可以使用按位运算符将所有内容存储为整数:
values[z][y][x] = intensity << 8 | distance;
intensity = values[z][y][x] >> 8;
distance = values[z][y][x] & 255;
But I wouldn't advise you to do so unless you're really savy with that value ranges (eg for saving bitmap/texture stuff). 但是我不建议您这样做,除非您真的很了解该值范围(例如,用于保存位图/纹理的东西)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.