std::collate<CharT>::hash, std::collate<CharT>::do_hash
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <locale> で定義
|
||
public: long hash( const CharT* beg, const CharT* end ) const; |
(1) | |
protected: virtual long do_hash( const CharT* beg, const CharT* end ) const; |
(2) | |
1) public メンバ関数。 最も派生したクラスの protected virtual メンバ関数
do_hash を呼びます。2) このロケールで同等と照合される (compare() が
0 を返す) すべての文字列に対して取得したハッシュと等しい整数値に文字シーケンス [beg, end) を変換します。 同等と照合されない2つの文字列について、それらのハッシュが等しい確率は非常に低い、具体的には 1.0/std::numeric_limits<unsigned long>::max() に近いべきです。引数
| beg | - | ハッシュするシーケンスの最初の文字へのポインタ |
| end | - | ハッシュするシーケンスの最後の次へのポインタ |
戻り値
照合順序を尊重したハッシュ値。
ノート
システム供給のロケールは、通常、 basic_string::operator== が false を返す場合、2つの文字列は同等と照合されません (compare() が 0 を返しません) が、ユーザが導入した std::collate ファセットは異なる照合ルールを提供しても良く、例えば文字列が同じ Unicode 正規化形式を持つ場合にそれらを同等と扱っても構いません。
例
ロケール対応の非順序コンテナをデモンストレーションします。
Run this code
#include <iostream>
#include <string>
#include <locale>
#include <unordered_set>
struct CollateHash {
template<typename CharT>
std::size_t 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';
}
出力例:
Bar Foo
関連項目
(C++11)(C++20)(C++11)(C++11)(C++11)(C++20)(C++20)(C++20)(C++20)(C++20) |
文字列に対するハッシュサポート (クラステンプレートの特殊化) |