std::collat...te::do_hash(3)
π std::collat...te::do_hash(3) β the group-chat cut
okay so real talk, bestie: this page is from the C++ Standard Library manual (the cppreference.com one, you know the vibe), and it's all about hashing strings the culturally aware way. sit down, grab a boba, we're learning π
β¨ NAME β¨
std::collate::hash, std::collate::do_hash β yes both of them, they're a package deal, no cap
π¦ Synopsis π¦
Both of these live in the header <locale>, iconically:
Defined in header <locale>
public: (1)
long hash( const CharT* beg, const CharT* end ) const;
protected: (2)
virtual long do_hash( const CharT* beg, const CharT* end ) const;
- the public member function π β what it does is call the protected virtual member function
do_hashof the most derived class. that's it, that's the whole job, she's a delegator and she's thriving - the protected virtual one π β it converts the character sequence
[beg, end)into an integer value that is equal to the hash obtained for all strings that collate equivalent in this locale (like,compare()returns0for them, iykyk). and for two strings that do not collate equivalent? the probability that their hashes are equal should be very small, approaching1.0 / std::numeric_limits<unsigned long>::max(). literally microscopic. not me doing probability math at 2am π
πΈ Parameters πΈ
begβ pointer to the first character in the sequence to hash πendβ one past the end pointer for the sequence to hash β¨
(yes bestie, "one past the end", that's the whole half-open range thing, it's giving classic C++)
π Return value π
the hash value that respects collation order. that's the tea. π«
π₯ Note π₯
okay this part actually matters so listen up π
the system-supplied locales normally do not collate two strings as equivalent (meaning compare() does not return 0) if basic_string::operator== returns false.
but β and this is the main character part β a user-installed std::collate facet may provide different collation rules. for example, it may treat strings as equivalent if they have the same Unicode normalized form. so like... your facet, your rules, your world π
π§ Example π§
this one demonstrates a locale-aware unordered container, which is honestly so slay of it
#include <iostream>
#include <locale>
#include <string>
#include <unordered_set>
struct CollateHash
{
template<typename CharT>
long operator()(const std::basic_string<CharT>& s) const
{
return std::use_facet<std::collate<CharT>>(std::locale()).hash(
&s[0], &s[0] + s.size()
);
}
};
struct CollateEq
{
template<typename CharT>
bool operator()(const std::basic_string<CharT>& s1,
const std::basic_string<CharT>& s2) const
{
return std::use_facet<std::collate<CharT>>(std::locale()).compare(
&s1[0], &s1[0] + s1.size(),
&s2[0], &s2[0] + s2.size()
) == 0;
}
};
int main()
{
std::locale::global(std::locale("en_US.utf8"));
std::wcout.imbue(std::locale());
std::unordered_set<std::wstring, CollateHash, CollateEq> s2 = {L"Foo", L"Bar"};
for (auto& str : s2)
std::wcout << str << ' ';
std::cout << '\n';
}
π Possible output π
Bar Foo
(emphasis on possible bestie, it's an unordered container, order is a vibe not a promise π)
β See also β
std::hash<std::basic_string>β hash support for strings,(C++11)(class template specialization) π₯
word of warning from your girl: the output really can come out in a different order, that's just unordered containers doing unordered-container things. no cap. π
okay that's the whole page, you ate that π
stay hydrated, go compile something cute, and remember: cppreference.com, 2024.06.10 β that's where this page came from and when it was stamped. mwah ππ«Ά
π¦ 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