Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.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++ 如何创建';n';以c+为单位调整数组大小+;?_C++_Arrays_Mingw - Fatal编程技术网

C++ 如何创建';n';以c+为单位调整数组大小+;?

C++ 如何创建';n';以c+为单位调整数组大小+;?,c++,arrays,mingw,C++,Arrays,Mingw,我正在尝试创建一个大小为n的数组,但每次尝试创建时,都会出现一个错误,即: expression must have a constant value -- the value of variable "n" (declared at line 33) cannot be used as a constant 我以前是这样工作的,但那是在代码块上。这次我手动下载了mingw,并且一直在使用VSCode。 我所有的其他代码都很好,但似乎找不到解决方案 int main()

我正在尝试创建一个大小为n的数组,但每次尝试创建时,都会出现一个错误,即:

expression must have a constant value -- the value of variable "n" (declared at line 33) cannot be 
used as a constant
我以前是这样工作的,但那是在代码块上。这次我手动下载了mingw,并且一直在使用VSCode。 我所有的其他代码都很好,但似乎找不到解决方案

int main()
{
    int n;
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    for (int i = 0; i < n; i++)
    {
        cout << arr[i];
    }
}

可以使用堆/动态数组创建长度为n的数组。 比如:
int arr=new array(n)

您可以使用heap/dynamic array创建长度为n的数组。 比如:
int-arr=新数组(n)

int-arr[n]无效C++,运行时<代码> N<代码>,使用VLA扩展。
改用
std::vector

int n;
std::cin >> n;
std::vector<int> arr(n);
intn;
标准:cin>>n;
std::向量arr(n);

int-arr[n]无效C++,运行时<代码> N<代码>,使用VLA扩展。
改用
std::vector

int n;
std::cin >> n;
std::vector<int> arr(n);
intn;
标准:cin>>n;
std::向量arr(n);

< /代码>既不是有效的C++,也不创建大小n.2的数组,它既不是有效的C++,也不创建大小n的数组。