我试图使用以下代码初始化一个字符串列表,在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;
}
I/O is
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'
可以结束一行。 - Pete Beckerl
中元素的数量,请调用l.size()
。sizeof
舞蹈只适用于 C 风格数组。 - Pete Becker