C ++:为什么int数组【尺寸】工作? [英] C++: Why does int array[size] work?
问题描述
我已经开始学习C ++。我读了前可以在运行时设置运行和dymanic数组的数组的大小只能进行设置。所以,我期待这失败,但事实并非如此:
I have started learning c++. I read that an array's size can only be set before run and dymanic arrays can be set during runtime. So I was expecting this to fail but it didn't:
#include <iostream>
int main() {
using namespace std;
int size;
cout << "enter array size:";
cin >> size;
int i, score[size], max; //array size set to variable doesn't fail
cout << endl << "enter scores:\n";
cin >> score[0];
max = score[0];
for (i = 1; i < size; i++)
{
cin >> score[i];
if (score[i] > max)
max = score[i];
}
cout << "the highest score is " << max << endl;
return 0;
}
这是在最近C ++编译器中的新功能?它是实现我需要一个动态数组并创建呢?
Is this a new feature in recent C++ compilers? Is it realising I need a dynamic array and creating that instead?
推荐答案
也许你正在使用GCC编译器,它具有称为扩展的变长的数组。
Probably you are using GCC compiler, it has an extension called Arrays of Variable Length.
的std ::矢量是在真正的动态数组C ++。
std::vector is the real dynamic arrays in C++.
要选择GCC这个标准,使用选项-std = C ++ 11;获得所有标准所要求的诊断,你也应该(如果你想他们是错误,而不是警告或-pedantic-错误)指定-pedantic。
To select this standard in GCC, use the option -std=c++11; to obtain all the diagnostics required by the standard, you should also specify -pedantic (or -pedantic-errors if you want them to be errors rather than warnings).
这篇关于C ++:为什么int数组【尺寸】工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!