std::unordered_set::rehash(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… NAME πŸ’…

std::unordered_set::rehash β€” bestie, it's the glow-up for your bucket count ✨

okay so real quick, the manual name is std::unordered_set::rehash and it lives in the C++ Standard Libary, and yes we are about to make your hash table move different πŸ’–


✨ Synopsis ✨

void rehash( size_type count );  (since C++11)

So what it does is it changes the number of buckets to some value n that is not less than count and satisfies

n >= size() / max_load_factor()

and then it rehashes the container β€” which is just a fancy way of saying it puts the elements into appropriate buckets considering that the total number of buckets has changed. like, everybody gets re-seated at the function, no cap πŸŽ€


🌸 Parameters 🌸

  • count β€” the lower bound for the new number of buckets. it's a floor, not a demand, iykyk πŸ¦‹

🫢 Return value 🫢

(none) β€” it gives you nothing back, it just does the thing. main character energy honestly πŸ’…


πŸ”₯ Complexity πŸ”₯

  • Average case: linear in the size of the container 🌈
  • Worst case: quadratic 😭

so like, usually slay, but worst case it's giving slow. plan accordingly bestie πŸ’€


πŸ“ Notes πŸ“

rehash(0) may be used to force an unconditional rehash β€” for real, zero is a valid move here πŸ‘‘

like, say you temporarily increased max_load_factor() and that suspended automatic rehashing? this is your little "ok we're back, rehash everything now" button. iconic ✨


🧚 See also 🧚

  • reserve β€” reserves space for at least the specified number of elements and regenerates the hash table (public member function) πŸ’œ

and that's the whole vibe check, bestie πŸ¦‹ this page was brought to you by http://cppreference.com (the source, ily) and it's dated 2024.06.10 🌸 go forth and rehash responsibly, mwah πŸ’–

πŸ“¦ where this page comes from