std::unorde...bucket_count(3)

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

πŸ’– NAME πŸ’–

std::unordered_multiset::max_bucket_count β€” yes bestie, it's literally the same name twice, that's just how cppreference does it, we love a consistent queen πŸ‘‘

So this page is the std::unordered_multiset::max_bucket_count page from the C++ Standard Library manual, section 3. And before you ask β€” the header says std::unorde...bucket_count(3) because the terminal clipped it, not because anything's wrong. She's fine. She's just long πŸ’…


✨ Synopsis ✨

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

Okay so this one's been serving since C++11 πŸŽ€

It returns the maximum number of buckets the container is able to hold, and why is that limited? Due to system or library implementation limitations. That's it. That's the whole vibe. It's not a "how many do I have" question (that's a different function, hold on), it's a "how many could I ever have" question 🌈


🌸 Parameters 🌸

Um. None. (none). Nada. Zero. She takes no arguments, she's low maintenance, we stan 🫢


⭐ Return value ⭐

The maximum number of buckets. That's the whole return value. Maximum. Number. Of. Buckets. πŸ”₯


πŸ¦‹ Complexity πŸ¦‹

Constant. O(1) baby, she's snappy, no cap πŸ’€βœ¨


πŸŽ€ Example πŸŽ€

// Run this code

#include <iostream>
#include <unordered_set>

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

    auto c1 = std::unordered_multiset<char>{};
    auto c2 = std::unordered_multiset<long>{};
    auto c3 = std::unordered_multiset<long, std::hash<int>>{};
    auto c4 = std::unordered_multiset<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'
        ;
}

So what's happening here β€” she makes four multisets with four different template parameter situations: one with char, one with long and the default hash, one with long but a different hash (std::hash<int>, iconique mismatch energy), and one with long and her own custom hasher Ha that just forwards to std::hash<long>. Then she prints all four max_bucket_count()s in hex with std::showbase so they look all 0x-y and gorgeous ✨


πŸ’€ Possible output πŸ’€

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

Notice how c4 is different?? That's the custom hasher doing her little thing β€” not me, that's just what the example shows πŸ™ˆ Read "possible" as possible, bestie, this is not a promise, it's a vibe check πŸ”₯


πŸ‘‘ See also πŸ‘‘

  • bucket_count β€” returns the number of buckets (public member function) β€” i.e. the actual count, not the theoretical max. Different girl, different job πŸ’œ

And that's the page!! 🌸 This lil beauty comes to you from cppreference.com, dated 2024.06.10, and it lives under the name std::unorde...bucket_count(3) in the manual. Go forth and count your theoretical buckets, you absolute main character πŸ§šπŸ’–

πŸ“¦ where this page comes from