
Lipik Contact escribió:
Hello list,
[...]
If the user holds a reference (or pointer) to some element in the container (e.g. T const *p = &*it), is that reference guaranteed to remain valid after the insertion/erase of some other elements? I ask because doc says that erasing through an iterator can be O(size) - which seems to suggest that erase can lead to a rehash, invalidating iterators. This appears to contradict the iterator lifetime guarantee posited by the standard (after all, tree based containers don't rebalance after erase, do they?)
For unordered containers, the standard says that insert members shall not affect the validity of references to container elements, but may invalidate all iterators to the container. The erase members shall invalidate only iterators and references to the erased elements. So a pointer to an inserted element remains valid until is erased. An iterator to an inserted object is invalidated after an insertion (because the insertion might have triggered a rehash operation). Erasures only invalidate iterators and pointers/references to erased elements. Regards, Ion