Load factor and bucket lists

Like Java's HashSet, std::unordered_set exposes all kinds of administrative details about its buckets. You probably will never need to interact with these administrative functions!

  • s.bucket_count() returns the current number of buckets in the array.
  • s.bucket(v) returns the index i of the bucket in which you'd find the element v, if it existed in this unordered_set.
  • s.bucket_size(i) returns the number of elements in the ith bucket. Observe that invariably s.count(v) <= s.bucket_size(s.bucket(v)).
  • s.load_factor() returns s.size() / s.bucket_count() as a float value.
  • s.rehash(n) increases (or decreases) the size of the bucket array to exactly n.

You might have noticed that load_factor seems out of place so far; ...

Get Mastering the C++17 STL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.