使用单个连续内存块为三维数组编制索引
Indexing a 3 dimensional array using a single contiguous block of memory
vector<bool> working_lattice(box.rect.length * box.rect.height * box.rect.width);
如何使用上述声明样式访问working_lattice[1][5][3]
?
您需要
将其访问为
(i * length * height) + (j * height) + k
所以在你的情况下
working_lattice[(i * box.rect.length * box.rect.height) + (j * box.rect.height) + k);
或
working_lattice[(1 * box.rect.length * box.rect.height) + (5 * box.rect.height) + 3);
编辑:既然你在其他地方提到了x,y,z
working_lattice[(x * box.rect.length * box.rect.height) + (y * box.rect.height) + z);
这取决于您使用的是行主排序还是列主排序。行主在 C/C++ 中更为典型,但如果手动执行,则可以执行其中任何一个。
在行主排序中,要到达第 i、j、k 个元素,您需要遍历box.rect.height * box.rect.width * i
元素才能到达第 i
行,加上box.rect.width * j
元素才能到达该行的第 j
列,再加上k
才能深度返回到第 k
个元素。超级明确地说:
const size_t n_x = box.rect.length;
const size_t n_y = box.rect.height;
const size_t n_z = box.rect.width;
working_lattice[1 * n_x * n_z + 5 * n_z + 3]
这显然很烦人,所以你可能想定义一个内联函数或其他东西来提供帮助。
即考虑这个:
A[R][S][T]
假设其基址为 addr_base_A
,
所以你希望你能得到一个特定元素的地址A[i][j][k]
,
我认为答案是:S*T*i + T*j + k + addr_base_A
.
希望这对:)有所帮助
相关文章:
- 多维数组存储三种不同的数据类型?
- 为什么我需要三个嵌套的大括号来调用赋值运算符,将const引用到二维数组
- 三维数组中的C/C++DWORD到BYTE和BYTE到DWORD的转换
- std::将三维数组复制到三维向量中
- 显示结构的三维数组
- 如何将X 2维数组连接到一个三维阵列中
- 使用unique_ptr来管理三维数组
- 无法访问三维数组中的特定位置
- 内存相关崩溃:Cocos2d游戏中的三维数组
- 如何在三维数组中使用std::map来存储/检索具有特定值的键
- 使用参数在C++中实例化三维数组
- 三维数组作为数组的向量
- 用C++将三维数组转换为二维数组
- C/C++ 三维数组如何在内存中存储,以及遍历它的最快方法是什么
- 三维数组c++中的访问冲突(使用malloc)
- 二维数组:二维数组的子平方的元素之和
- 用变量作为C++中二维数组的维数的问题
- 读取数组2维的另一种方式c++
- 有四个括号的数组的维数是多少?
- 在c++中实现多维数组奇维的最佳方法