Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use std::vector.erase(begin(), end()) or std::vector.erase(begin(), begin())?

Tags:

c++

stl

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())?

like image 489
Mircea Ispas Avatar asked Jan 15 '23 18:01

Mircea Ispas


1 Answers

Yes, this is correct - that's what the notation [first, last) means and end() points one past the end.

like image 160
djechlin Avatar answered Feb 06 '23 13:02

djechlin