Search code examples
c++iteratorcontainerserase

Does std::list's end() iterator position change when erasing?


In the following loop I'm using a pre-calculated end iterator:

std::list::iterator end = MyList.end();

for (std::list::iterator it = MyList.begin(); it != end ;) 
    it = MyList.erase(it);

When erasing an element in a std::list, can the MyList.end() change its value so that end != MyList.end() anymore?


Solution

  • No.

    n3376 23.3.5.4

    iterator erase(const_iterator position);
    iterator erase(const_iterator first, const_iterator last);
    

    Effects: Invalidates only the iterators and references to the erased elements.