Why does map.erase return an iterator?

I want to erase std :: map elements from beginIt to endIt. The erase function returns an iterator to the element that follows the last deleted element. Isn't that the end? Why does erase return an iterator?

auto it = m_map.erase(beginIt, endIt); 
+5
source share
2 answers

This is a useful feature that the C ++ standard library uses for all of its containers.

In particular, it is useful to use when you delete a set of elements that are subject to restriction and iterate over the entire container. Obviously, removing anything from the container invalidates the iterator you passed. To return the next candidate iterator is useful.

+8
source

I believe this is due to an attempt to unify function calls across standard container types. For example, in std::vector returned iterator does not match endIt

+2
source

Source: https://habr.com/ru/post/1245724/


All Articles