Reputation: 111
I've want to erase the elements of the std::map from beginIt to endIt. erase function returns the iterator to the element that follows the last element removed. isn't it endIt ? Why the erase returns iterator ?
auto it = m_map.erase(beginIt, endIt);
Upvotes: 7
Views: 2306
Reputation: 234715
It's a useful feature that the C++ standard library adopts for all its containers.
One good use in particular is when you are deleting a set of elements subject to a constraint and you are iterating over the whole container. Obviously deleting something from a container invalidates the iterator that you passed. To return the next candidate iterator is useful.
Upvotes: 9
Reputation: 60052
I believe this is due to trying to unify function calls across the standard container types. For example, in an std::vector
the returned iterator is not the same as endIt
Upvotes: 2