i want to traverse a list and delete elements greater than 30
QList<double> mylist;
QList<double>::iterator myiterator = mylist.begin();
mylist.append(6);
mylist.append(36);
myiterator=mylist.begin();
while(myiterator!=mylist.end())
{
if(*(myiterator)>30)
{
QList<double>::iterator next=myiterator;
next++;
mylist.erase(myiterator);
myiterator=next;
}
else
myiterator++;
if(myiterator==mylist.end())
std::cout<<"end of list "<<std::endl;
else
std::cout<<" not end of list "<<std::endl;
}
output is
not end of list
not end of list
The program has unexpectedly finished.
/home/maverik/Desktop/Qt/container_class/container_class exited with code 0
where as if i make mylist = {36,34} or mylist = {36,9} or mylist={9,10} then output is
not end of list
end of list
the error only comes when the last element is greater than 30 or if greater but the 1st element is also greater than 30
Like the C++ standard containers
erasereturns an iterator to the element after the deleted one, so that part of your loop should be quite simple: