在堆C ++多维数组 [英] C++ Multi-dimensional Arrays on the Heap

查看:136
本文介绍了在堆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.

这篇关于在堆C ++多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆