search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

2 answers
Q&A c++为什么不支持直接创建动态的多维数组?
c++为什么不支持直接创建动态的多维数组?
大家讲道理 2016-11-11 13:52:48 Views 1555
Go

0

C/C++为什么不支持直接创建动态的多维数组呢?如果要需要一个二维数组int dpsize1, size1和size2都是在程序运行中确定的。采用

int[][] dp = new int[size1][size2];

的方式,会报错“expected unqualified-id before '[' token”。

对这类需求可以通过

int** dp = new int*[size1]for (int i = 0; i 

的方式来创建,但是这样得到的数组在创建是比较麻烦,内存空间也不一定是连续的。为什么c/c++语言不支持直接采用new ints[s]的方式来创建多维数组呢?


Your Answer
submit

2 answers
0

为什么c/c++语言不支持直接采用new ints[s]的方式来创建多维数组呢?

因为标准没支持,所以不支持

动态的创建多维数组除了你上面写的那种还可以直接new int[size1 * size2],然后以偏移来访问,如果不要求连续,还有模板可以用vector> d2array


2016-11-11 13:53:59

submit

0

其实现在的C标准是兹磁的,你可以这样写

int *old_dp = malloc(sizeof(int) * size1 * size2);
int (*dp)[size2] = (int(*)[size2])(old_dp);
// 然后直接写dp[i][j]就可以啦,注意不要越界

c++的话确实是标准不兹磁,如果实在想追求效率的话只能写个函数包装一下了


2016-11-11 13:54:19

submit

Popular tool

vc9-vc14 (32+64 bit) runtime library collection (link below)

vc9-vc14 (32+64 bit) runtime library collection (link below)

Download the collection of runtime libraries required for phpStudy installation

VC9 32-bit

VC9 32-bit

VC9 32-bit phpstudy integrated installation environment runtime library

PHP programmer toolbox full version

PHP programmer toolbox full version

Programmer Toolbox v1.0 PHP Integrated Environment

VC11 32-bit

VC11 32-bit

VC11 32-bit phpstudy integrated installation environment runtime library

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use