I understand the
unordered_ stl containers keep a number of buckets that vary in number depending on the number of elements in the container. When inserting, if breaking a certain limit, the container will rehash to use more buckets, so each bucket is less full and faster to search. And that invalidates the iterators.
This means I shouldn't keep iterators to an
unordered container. Except I can, if I update them after a rehash. But I couldn't find a reliable way to check whether an
insert (be it
emplace or whatever) caused a rehash.
Should I monitor
Rehashing occurs only if the new number of elements is greater than max_load_factor()*bucket_count(). Is that garanteed? Would it be reliable to do the following?
bool will_rehash = (max_load_factor()*bucket_count()) > size()+1;