在堆C ++多维数组
问题描述:
我怎么会去动态分配一个多维数组?
How would I go about dynamically allocating a multi-dimensional array?
答
如果您知道嵌套维度的尺寸已,还可以从字面上使用分配一个多维数组新:
If you know the size of nested dimensions already, you can also literally allocate a multi dimensional array using new:
typedef int dimensions[3][4];
dimensions * dim = new dimensions[10];
dim[/* from 0 to 9 */][/* from 0 to 2 */][/* from 0 to 3 */] = 42;
delete [] dim;
而不是 10
,运行时确定的值可以传递。因为它不是类型的运营商新的收益的一部分,这是允许的。如果你知道的列数,但要保留行变量的数量,例如这是很好的。该类型定义可以更容易地读取code。
instead of 10
, a runtime determined value can be passed. Since it's not part of the type operator new returns, that's allowed. This is nice if you know the number of columns, but want to keep the number of rows variable, for example. The typedef makes it easier to read the code.