Just so I'm clear, the iterator isn't invalidated by pop_back(), except when the iterator is pointing at the last element, right?
std::vector::pop_back() - The end iterator and any iterator, pointer and reference referring to the removed element are invalidated.
Iterators, pointers and references referring to other elements that have not been removed are guaranteed to keep referring to the same elements they were referring to before the call.
So only if there is just one element left, then it will crash. I'm not seeing how it will crash on the first element.
So, would this work:
for(ZombieIterator it = zombies.begin(); it != zombies.end();)
{
if(it->dead)
{
//Swap the value of the current zombie with the zombie at the back of the container.
std::swap(*it, zombies.back());
//Pop the back of the container.
zombies.pop_back();
if(zombies.empty())
{
break;
}
}
else
{
++it;
}
}
Or am I just digging myself deeper? ![]()
Edited by Servant of the Lord, 21 January 2013 - 05:31 PM.






