std::unorde...ucket_count(3)

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

πŸ’… std::unordered_map::max_bucket_count πŸ’…

okay bestie so this page lives in the C++ Standard Library manual (you already knew that, you're iconic) β€” and today's main character is std::unordered_map::max_bucket_count πŸŽ€

No cap, this one is a lil cutie. Let's get into it ✨


🌸 NAME 🌸

std::unordered_map::max_bucket_count β€” like, literally std::unordered_map::max_bucket_count πŸ’–


πŸ¦‹ Synopsis πŸ¦‹

The ✨vibe✨ is:

size_type max_bucket_count() const;  (since C++11)

So what it does is β€” it hands you back the maximum number of buckets the container can even hold, because of system or library implementation limitations. That's it, that's the whole personality 🫢

ngl "system or library implementation limitations" is doing a lot of heavy lifting there β€” it just means your computer / your stdlib says "bestie this is my ceiling" πŸ’€

πŸ’œ Parameters πŸ’œ

(none) β€” she takes nothing, she needs nothing, she is unbothered πŸ‘‘


⭐ Return value ⭐

The maximum number of buckets. That's the whole thing. Slay 🌈


🧚 Complexity 🧚

Constant. Fast. Instant. No drama, no waiting, we love a low-effort queen πŸ’…


πŸ“ Example πŸ“

Run this code, fr fr:

#include <iostream>
#include <unordered_map>

int main()
{
    struct Ha { std::size_t operator()(long x) const { return std::hash<long>{}(x); }; };

    auto c1 = std::unordered_map<char, long>{};
    auto c2 = std::unordered_map<long, long>{};
    auto c3 = std::unordered_map<long, long, std::hash<int>>{};
    auto c4 = std::unordered_map<long, long, Ha>{};

    std::cout
        << "Max bucket count of\n" << std::hex << std::showbase
        << "c1: " << c1.max_bucket_count() << '\n'
        << "c2: " << c2.max_bucket_count() << '\n'
        << "c3: " << c3.max_bucket_count() << '\n'
        << "c4: " << c4.max_bucket_count() << '\n'
        ;
}

Like β€” four whole maps, four different template setups, all just flexing their bucket ceilings 😭 and yes, we're printing in hex with std::showbase because we're classy like that πŸ’…

Possible output:

Max bucket count of
c1: 0xfffffffffffffff
c2: 0xfffffffffffffff
c3: 0xfffffffffffffff
c4: 0xaaaaaaaaaaaaaaa

Not c4 pulling up with a completely different number and zero explanation 😭 β€” anyway, that's what you get, taste the rainbow 🌈


πŸ”₯ See also πŸ”₯

  • bucket_count β€” returns the number of buckets (public member function) β€” like her chiller, more chillaxed cousin, iykyk πŸ‘‘

okay that's the whole page, love u, go write some hash maps πŸ’– β€” this one was published on cppreference over at http://cppreference.com, dated 2024.06.10, and it's the std::unorde...ucket_count(3) page in the C++ Standard Libary manual. mwah, see u in the next one πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from