dimanche 31 juillet 2022

Are iterators still valid when the underlying elements have been moved?

If I have an iterator pointing to an element in an STL container, and I moved the element with the iterator, does the standard guarantee that the iterator is still valid? Can I use it with container's method, e.g. container::erase?

Also does it matter, if the container is a continuous one, e.g. vector, or non-continuous one, e.g. list?

std::list<std::string> l{"a", "b", "c"};
auto iter = l.begin();
auto s = std::move(*iter);
l.erase(iter);       // <----- is it valid to erase it, whose underlying element has been removed?

Aucun commentaire:

Enregistrer un commentaire