Search code examples
c++vectorerase

Is passing vector's end() to std::vector::erase valid?


A very simple question but I couldn't find an answer. It would make awful lot of sense for it to be allowed but want to double-check.

std::vector<int> v(10, 0);
v.erase(v.end()); // allowed or not?

Solution

  • An invalid position or range causes undefined behavior.

    From here

    The iterator pos must be valid and dereferenceable. Thus the end() iterator (which is valid, but is not dereferencable) cannot be used as a value for pos.