我正在尝试使用以下代码初始化C++ 11中的字符串列表,以及由于各种原因而失败。错误说我需要使用构造函数来初始化列表,我应该使用类似list<string> s = new list<string> [size]
的东西吗?我在这里错过了什么?初始化C++中的字符串列表11
#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;
}
I/O是
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"};
这是没有问题的,但你真的需要额外的东西,'STD: :endl'呢? ''\ n''结束一行。 –
要获得列表'l'中的元素数目,请调用'l.size()'。这个'sizeof'舞蹈只适用于C风格的数组。 –
您的错误消息似乎是告诉你,你正在用C++ 98而不是11 –