I want to process elements from a vector for some time. To optimize this I don't want to remove an item when it is processed and I remove all processed items at the end.
vector<Item*>::iterator it;
for(it = items.begin(); it != items.end(); ++it)
{
DoSomething(*it);
if(TimeIsUp())
{
break;
}
}
items.erase(items.begin(), it);
Is it safe to use erase when it == items.end()
? In documentaion it is said that erase()
will erase [first,last) and this should be safe but I want to be sure.
EDIT:
Is it safe to use std::vector.erase(begin(), begin())?
Yes, this is correct - that's what the notation [first, last)
means and end()
points one past the end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With