Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;阵列:从一维到二维_C++ - Fatal编程技术网

C++ C++;阵列:从一维到二维

C++ C++;阵列:从一维到二维,c++,C++,我有一个整数数组: int a[1]; 然后我想让a中的项目成为指向数组下一维度的指针 a[0] = new int b[3]; 然后我能使用双括号索引吗 a[0][0] = 1; //Assign 1 to b[0] 是的,但必须使成为一个指针数组: int *a[1]; 如果要将中的项设置为数组,则必须以这种方式进行定义 int *a[1]; a[0] = new int[3]; 或者类似地 int a[1][3]; 或 但是由于您有c++标记,您应该使用std::vector(

我有一个整数数组:

int a[1];
然后我想让
a
中的项目成为指向数组下一维度的指针

a[0] = new int b[3];
然后我能使用双括号索引吗

a[0][0] = 1; //Assign 1 to b[0]

是的,但必须使
成为一个
指针数组:

int *a[1];

如果要将
中的项设置为
数组,则必须以这种方式进行定义

int *a[1];
a[0] = new int[3];
或者类似地

int a[1][3];


但是由于您有
c++
标记,您应该使用
std::vector
(或者
c++11
中的
std::array
)。

我没有建议,而是回答了这个问题。OP显然在学习。在某个时候,为了理解模板和多态性的所有怪癖,他必须学习原始指针。这是一个开始学习的好地方。不,他不会。你可以先做模板。特别是说,
std::vector
int **a;
a = new int *[1];
a[0] = new int[3];