当我像这样在 C++ 中初始化一个向量时:
#include <string>
#include <vector>
using std::vector;
using std::string;
int main() {
vector<int> v1 = {1, 2, 3, 4};
return 0;
}
我收到此错误:
prog1.cpp:8:15: error: non-aggregate type 'vector<int>' cannot be initialized with an initializer list
vector<int> v1 = {1, 2, 3, 4};
^ ~~~~~~~~~~~~
1 error generated.
知道为什么会这样吗?
初始化列表是从 C++ 11 开始引入的,因此-std=version
如果您使用旧标准进行默认编译,则需要在编译 ( ) 时指定该(或更高)版本。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句