我试图使用以下代码在 c++11 中初始化字符串列表,但由于各种原因而失败。错误说我需要使用构造函数来初始化列表,我应该使用 list<string> s = new list<string> [size]
类的东西吗?我在这里想念什么?
#include<string>
#include<list>
#include<iostream>
using namespace std;
int main() {
string s = "Mark";
list<string> l {"name of the guy"," is Mark"};
cout<<s<<endl;
int size = sizeof(l)/sizeof(l[0]);
for (int i=0;i<size;i++) {
cout<<l[i]<<endl;
}
return 0;
}
输入/输出是
strtest.cpp:8:47: error: in C++98 ‘l’ must be initialized by constructor, not
by ‘{...}’
list<string> l {"name of the guy"," is Mark"};
原文由 Aparna Chaganti 发布,翻译遵循 CC BY-SA 4.0 许可协议
您使用的是 c++98 而不是 c++11 的编译器。如果您使用的是 gcc,请使用它
你可以用 gnu++11 替换 c++ 11