是否在c ++中创建了允许运行时边界的数组?

vik*_*ghe 2 c++ arrays runtime variable-declaration

根据 C++: 在c ++中不允许创建一个用户输入的大小来创建具有运行时边界的数组.

但是我得到的代码编译没有错误.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {


    int n;
    cin>>n;

    int a[n][n];
    a[n-1][n-1]=9;
    cout<<a[n-1][n-1]<<endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它也很好用.在这里查看 - > http://cpp.sh/6bies

有人可以帮助解决这种混乱吗?

eer*_*ika 6

是否在c ++中创建了允许运行时边界的数组?

这种阵列是不正确的.

但是我得到的代码编译没有错误.......它也很好用.

C++标准不禁止编译器成功编译不正确的程序.显示诊断消息就足够了.这允许编译器扩展语言.

如果你看看你的例子的编译器输出,你会发现编译器确实告诉你它,正如C++标准所要求的那样:

15:15: warning: array of array of runtime bound [-Wvla]
Run Code Online (Sandbox Code Playgroud)

因此,您的编译器似乎支持运行时绑定的数组 - 甚至是运行时绑定数组的数组 - 作为语言扩展.

如果您愿意,可以要求大多数编译器拒绝根据C++标准构造不良的程序.


它是标准兼容的,用于创建动态存储中绑定的运行时数组.最简单的方法是使用std::vector.