std::hash>(3)

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

πŸ’– NAME πŸ’–

okay so bestie, this is the std::hash>(3) page from the C++ Standard Libary β€” yes, that's the manual name, typo and all, and we are keeping it because it's iconic πŸ’…. The NAME section is literally just:

  • std::hash> β€” std::hash>

That's it. It's giving "named it twice and still slayed" ✨.


✨ Synopsis ✨

Defined in header <vector>.

Since C++11, there's this template specialization:

template< class Allocator >
struct hash<std::vector<bool, Allocator>>;

Lowkey the whole point: this specialization of std::hash for std::vector<bool> allows users to obtain hashes of objects of type std::vector<bool>. That's the main character energy β€” it makes hashing a vector<bool> possible. No cap 🫢.


πŸ’» Example πŸ’»

Run this code, bestie:

// Run this code

#include <iostream>
#include <unordered_set>
#include <vector>

using vb = std::vector<bool>;

vb to_vector_bool(unsigned n)
{
    vb v;
    do
    {
        v.push_back(n & 1);
        n >>= 1;
    }
    while (n);
    return v;
}

auto print(const vb& v, bool new_line = true)
{
    for (std::cout << "{ "; const bool e : v)
        std::cout << e << ' ';
    std::cout << '}' << (new_line ? '\n' : ' ');
}

int main()
{
    for (auto i{0U}; i != 8; ++i)
    {
        std::cout << std::hex << std::uppercase;
        vb v = to_vector_bool(i);
        std::cout << std::hash<vb>{}(v) << ' ' << std::dec;
        print(v);
    }

    // std::hash for vector<bool> makes it possible to keep them in
    // unordered_* associative containers, such as unordered_set.

    std::unordered_set v{vb{0}, vb{0, 0}, vb{1}, vb{1}, vb{1, 0}, vb{1,
       1}};

    for (vb const& e : v)
        print(e, 0);
    std::cout << '\n';
}

🫧 Possible output 🫧

Here's the possible output vibe:

6D09EE26D5863619 { 0 }
3C27D9F591D20E49 { 1 }
E74D3F72B7599C63 { 0 1 }
EE3BE81F55123770 { 1 1 }
3AAD2A2EDBEC6C35 { 0 0 1 }
EB057F773CB64C43 { 1 0 1 }
6E1354730102BE00 { 0 1 1 }
E2E622597C18899D { 1 1 1 }
{ 1 1 } { 1 0 } { 1 } { 0 0 } { 0 }

πŸ‘€ See also πŸ‘€

  • hash β€” hash function object
    • (C++11) β€” (class template)

that's the whole page, bestie 🫢 β€” source http://cppreference.com, version/date 2024.06.10, and the footer also repeats std::hash>(3). periodt πŸ’…

πŸ“¦ where this page comes from