Using C++11 or higher, suppose we store an iterator pointing to an element in a vector:
std::vector<int> v{1, 2, 3, 4, 5};
auto it = v.begin() + 2; // it points to 3
Now, erasing an element before or including the pointed-to element will invalidate it
. I am assuming erasing an element does not cause memory reallocation (maybe incorrectly?), so would it then be valid and well-defined behavior to correct it
as follows:
--it;
v.erase(v.begin());
// it still points to 3 in all cases?
Godbolt: https://godbolt.org/z/3dzrcW
Aucun commentaire:
Enregistrer un commentaire