Neeraj Singh Aithani Neeraj Singh Aithani - 3 months ago 9
C++ Question

How to declare a vector of llist of string and process it?

I am new in c++. I have tried the following but i am getting error please correct me.

vector<list<string>> ques(size); //initialization
cout<<ques[pos].front()<<endl; //printing out the top element at pos
vector<list<string>>::iterator it;
for(it=ques[pos].begin();it!=ques[pos].end();it++) //traversing through the list at pos <getting error>
{
cout<<*it;
}





error: no match for ‘operator=’ (operand types are ‘std::vector<std::list<std::basic_string<char> > >::iterator {aka __gnu_cxx::__normal_iterator<std::list<std::basic_string<char> >*, std::vector<std::list<std::basic_string<char> > > >}’ and ‘std::list<std::basic_string<char> >::iterator {aka std::_List_iterator<std::basic_string<char> >}’)|

Answer

You are iterating over a std::list<std::string>.

This means that your iterator should be:

list<string>::iterator it;

and not:

vector<list<string>>::iterator it;

This would be for iterating over the vector itself.

Furthermore, it's even better to use modern C++, which makes the compiler figure this out, all by itself:

for(auto it=ques[pos].begin(); it!=ques[pos].end(); it++)

and even better:

for (auto &value: ques[pos])
     cout << value;
Comments