I was trying to initialize a list of strings in c++11 using the following code, and its failing with various reasons. The error says that I need to use constructor to initialize the list, should I use something like list<string> s = new list<string> [size]
? What am I missing here?
#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
does?'\n'
ends a line.l
, calll.size()
. Thatsizeof
dance only works for C-style arrays.