I found this in the standard as the post-condition for the rehash function in unordered associative containers :

Post: a.bucket_count() > a.size() / a.max_load_factor() and a.bucket_count() >= n. (n being the number of buckets in the container)

Can I take the above to mean that an automatic rehashing is triggered when either of the above conditions is met for all implementations? Or, are implementations free to decide when to rehash and the above only pertains to the rehash function?

有帮助吗?

解决方案

The implementation shall keep the load_factor() <= max_load_factor() and load_factor() == size() / bucket_count(). So automatic rehashing can occur during an insert to keep the load factor invariant.

Although the load_factor() can not exceed max_load_factor(), I don't think there is a guarantee that no rehashing will be done during an insert even if you can prove that this invariant will not be violated.

Definition for max_load_factor is:

Returns a positive number that the container attempts to keep the load factor less than or equal to. The container automatically increases the number of buckets as necessary to keep the load factor below this number.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top