§23.1.2.8 in the standard states that insertion/deletion operations on a set/map will not invalidate any iterators to those objects (except iterators pointing to a deleted eleme
I had a similar question recently, but I was wondering if calling end()
to retrieve an iterator for comparison purposes could possibly have race conditions.
According to the standard, two iterators are considered equivalent if both can be dereferenced and &*a == &*b
or if neither can be dereferenced. Finding the bolded statement took a while and is very relevant here.
Because an std::map::iterator
cannot be invalidated unless the element it points to has been removed, you're guaranteed that two iterators returned by end
, regardless of what the state of the map was when they were obtained, will always compare to each other as true.