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_b ack(1);
the_list.push_b ack(2);
the_list.push_b ack(3);
the_list.push_b ack(4);
list<int>::iter ator 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_b ack(1);
the_list.push_b ack(2);
the_list.push_b ack(3);
the_list.push_b ack(4);
list<int>::iter ator 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?
Comment