1

这听起来可能很疯狂,但我想知道是否可以让程序在使用 C/C++ 的循环中声明n该类型的数组的数量。array[]例如,示例以下伪代码:

input int _n_  

run loop for _n_ times such that:  
declare _array1[]_  
declare _array2[]_  
.  
.  
declare _array'n'[]_ 

所以这里的问题有两个方面:
- 声明可变长度数组
- 声明一个可变数量(即n个)这样的数组。

4

2 回答 2

4

真值表:

task        / language         | C                   | C++
-------------------------------+-----------------------+------------------------
Declare variable length arrays | Use VLAs            | not possible without
                               |      int arr[n];    | non-standard extensions
                               |                     | but use std::vector<T>
-------------------------------+---------------------+--------------------------
Declare a variable number      |  not possible but   | not possible but use
(i.e. n number of) such arrays |  use int arr[n][k]; | vector<vector<T>> 
于 2013-10-06T16:10:02.770 回答
2

我理解它的方式,如果你想要多个数组,你不能只使用二维数组吗?这当然意味着您没有可变长度的数组,但是您可以拥有可变数量的具有相同长度的数组。

然后你有这个:

int n;
int array[n][length];
于 2013-10-06T16:11:07.137 回答