std::hash>(3)
π 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms