M
Martin Magnusson
I know a similar question was recently posted here, but after trying the
solutions in that thread I still have problems.
list<int> the_list;
the_list.push_back(1);
the_list.push_back(2);
the_list.push_back(3);
the_list.push_back(4);
list<int>::iterator i = the_list.end();
i--;
cout << the_list.size();
for (std::list<int>::iterator j = i;
j != the_list.end();
++j)
{
cout << ".";
j = the_list.erase( j );
}
cout << the_list.size();
What happens is this: j goes to the end of the list and erases the last
element. By then I would have thought that j would be set to nodes.end()
and the loop would terminate, but instead it seems that the loop keeps
running, erasing all of the list. The output of the above code is
"4....0". What am I doing wrong?
solutions in that thread I still have problems.
list<int> the_list;
the_list.push_back(1);
the_list.push_back(2);
the_list.push_back(3);
the_list.push_back(4);
list<int>::iterator i = the_list.end();
i--;
cout << the_list.size();
for (std::list<int>::iterator j = i;
j != the_list.end();
++j)
{
cout << ".";
j = the_list.erase( j );
}
cout << the_list.size();
What happens is this: j goes to the end of the list and erases the last
element. By then I would have thought that j would be set to nodes.end()
and the loop would terminate, but instead it seems that the loop keeps
running, erasing all of the list. The output of the above code is
"4....0". What am I doing wrong?