c++ - 如何使程序声明可变数量的不确定大小的数组

标签 c++ c arrays loops variable-length-array

这听起来可能很疯狂,但我想知道是否有可能让一个程序使用 C/C++。例如,对这个伪代码进行采样:

input int _n_  

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

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

最佳答案

真值表:

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>> 

关于c++ - 如何使程序声明可变数量的不确定大小的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19210773/

相关文章:

c++ - setenv和getenv文档

c++ - 我可以创建一个可以复制任意大小的二维数组的 'CopyArray' 函数吗?

c++ - Antlr C 运行时示例中的 undefined reference

c - 使用 Park&Miller RNG 制作大样本?

c - 了解返回值函数 C

c++ - 如何创建指向字节数组的指针?

c++ - 当我 push_back 到 vector 时调用析构函数

c - 我的代码使用除法运算符不起作用

javascript - LeetCode - 为什么这个函数返回未定义,尽管它刚刚记录了一个数组?

java - 如何找到数组的所有连续子数组组合并打印它