F
Frank Neuhaus
Hi
I have some std list, I'd like to traverse. During the traversal, I want to
conditionally delete some objects.
My code for that is like this right now:
for (std::list<myStruct>::iterator it=myList.begin();it!=myList.end()
{
if (it->someCondition)
{
std::list<myStruct>::iterator it2=it;
++it;
myList.erase(it2);
continue;
}
// .. do some work on the myStructs that arent supposed to be deleted
yet...
++it;
}
Is there any more elegant solution for my problem? It's quite ugly code
right now imho.
Simply using myList.erase(it++); doesnt work because after the erase line,
the iterator isnt valid anymore and thus cant be increased anymore.
Thanks alot
I have some std list, I'd like to traverse. During the traversal, I want to
conditionally delete some objects.
My code for that is like this right now:
for (std::list<myStruct>::iterator it=myList.begin();it!=myList.end()
{
if (it->someCondition)
{
std::list<myStruct>::iterator it2=it;
++it;
myList.erase(it2);
continue;
}
// .. do some work on the myStructs that arent supposed to be deleted
yet...
++it;
}
Is there any more elegant solution for my problem? It's quite ugly code
right now imho.
Simply using myList.erase(it++); doesnt work because after the erase line,
the iterator isnt valid anymore and thus cant be increased anymore.
Thanks alot