对在运行时确定大小的数组的引用
reference to an array of size determined at run-time
我试图找到这个,但找不到任何。我知道我可以创建一个数组变量的引用:
int x[10] = {}; int (&y)[10] = x;
但是,在编译时不知道数组大小的情况下,如以下代码:
const int n = atoi( string ); //the string is read from a text file at run time.
int x[n] = {}; int (&y)[n] = x; //this generates a compiling error.
即使int n被声明为const,只要n在编译时未知,引用就是无效的。编译器会这样说:对类型'int [n]'的引用不能绑定到不相关类型'int [n]'的值。有人知道怎么解决这个问题吗?
运行时长度数组是C99的一个特性,在标准c++中不存在。它们作为扩展出现在一些c++编译器上,但不能很好地与c++特性混合,比如引用和模板。
动态声明数组的特性不应该在c++中使用。并非所有编译器都支持它。考虑使用STL容器。如std::vector<int>
相关文章:
- 通过指针与数组引用扩展数组索引序列
- 将链表的内容放在数组引用中
- 是否允许使用初始值设定项列表将const数组引用实例化为构造函数参数
- C 在将数组引用分配给struct时麻烦
- Typedef数组引用
- 通过强制转换创建子数组引用
- 将数组引用 'T(&)[n]' 引用到 'std::array<T, n>' 的内容
- constexpr 数组引用参数
- 寻找有关函数定义和原型中数组引用的说明
- Initializer_list作为非模板上下文中数组引用参数的参数
- 如何使用一个动态创建的一维数组的二维数组引用仅与标准库?
- 如何返回具有大小的POD数组引用
- 在C++中返回数组引用的语法
- 是否可以声明转换函数返回数组引用而不使用typedef
- 自定义到数组引用的转换
- 类数组引用的NullReferenceException
- 数组指针与数组引用之间的差异
- 在c++中传递数组引用
- 使用特征矩阵作为特征数组引用的参数
- 如何从函数传递数组引用