std::hash,s...,std::hash,(3)
π std::hash for string_view besties π
Okay so this is the C++ Standard Libary man page, and the page label is literally std::hash,s...,std::hash,(3) β yes, the commas are having a moment. We're talking about the std::hash specializations for the string view family. It's giving βhash me, keep me in an unordered container, and don't let the referenced string dangle.β π
πΈ NAME πΈ
std::hash,std::hash,std::hash, β std::hash,std::hash,std::hash, (that's the literal NAME, bestie; we are not fixing it, we are respecting the chaos).
β¨ Synopsis β¨
Defined in header <string_view>:
template<> struct hash<std::string_view>; (since C++17)
template<> struct hash<std::wstring_view>; (since C++17)
template<> struct hash<std::u8string_view>; (since C++20)
template<> struct hash<std::u16string_view>; (since C++17)
template<> struct hash<std::u32string_view>; (since C++17)
These are the template specializations of std::hash for the various view classes, made for hashing views. Periodt. π«Ά
And the slay detail: these hashes equal the hashes of the corresponding std::basic_string classes. If S is one of the standard basic_string types, SV is the corresponding string view type, and s is an object of type S, then:
std::hash<S>()(s) == std::hash<SV>()(SV(s))
No cap, that's the guarantee. π
π₯ Example π₯
// Run this code β literally, go off:
#include <iostream>
#include <string_view>
#include <unordered_set>
using namespace std::literals;
int main()
{
std::cout << "\"A\" #: " << std::hash<std::string_view>{}("A"sv) << '\n';
std::cout << "L\"B\" #: " << std::hash<std::wstring_view>{}(L"B"sv) << '\n';
std::cout << "u8\"C\" #: " << std::hash<std::u8string_view>{}(u8"C"sv) << '\n';
std::cout << "u\"D\" #: " << std::hash<std::u16string_view>{}(u"D"sv) << '\n';
std::cout << "U\"E\" #: " << std::hash<std::u32string_view>{}(U"E"sv) << '\n';
// std::hash for string_view family makes it possible to keep these view-types
// in unordered_* associative containers, such as unordered_set. But ensure
// the lifespan of referenced strings is no less than lifespan of the container,
// i.e. no dangling references occurred.
std::unordered_set stars{"Rigel"sv, "Capella"sv, "Vega"sv, "Arcturus"sv};
for (std::string_view const& s : stars)
std::cout << s << ' ';
std::cout << '\n';
}
π Possible output π
"A" #: 6919333181322027406
L"B" #: 11959850520494268278
u8"C" #: 12432341034569643010
u"D" #: 312659256970442235
U"E" #: 18073225910249204957
Arcturus Vega Capella Rigel
π See also π
hashβ hash function object(C++11)(class template)std::hash<std::basic_string>β hash support for strings(C++11)(class template specialization)
Source: http://cppreference.com Β· version/date: 2024.06.10 Β· page id: std::hash,s...,std::hash,(3). Bye bestie, stay hashed and hydrated π«Άβ¨
π¦ 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