【发布时间】:2019-01-19 06:07:48
【问题描述】:
我很惊讶下面的代码编译并显示正确的结果。
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
char arr[v.size()]; /* why does this line compile? */
std::cout << sizeof(arr) / sizeof(arr[0]) << std::endl; /* display 3 */
return 0;
}
我认为在 C/C++ 中定义数组时必须使用恒定长度。 vector::size() 是在运行时确定的,为什么会这样呢?
【问题讨论】:
标签: c++