相关疑难解决方法(0)

动态分配3d数组

关于动态分配3d数组我有点困惑.现在,我只是像这样分配一大块内存:

int height = 10;
int depth = 20;
int width = 5;

int* arr;
arr = new int[height * width * depth];
Run Code Online (Sandbox Code Playgroud)

现在我想更改3D数组中的值,例如:

//arr[depth][width][height]
arr[6][3][7] = 4;
Run Code Online (Sandbox Code Playgroud)

但是,我无法使用上面的代码来更改值.如何使用单个索引访问位置深度= 6,宽度= 3,高度= 7的元素?

arr[?] = 4;
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来动态分配3D数组?

c++ dynamic multidimensional-array

3
推荐指数
4
解决办法
2万
查看次数

标签 统计

c++ ×1

dynamic ×1

multidimensional-array ×1