std::collate<CharT>:: hash, std::collate<CharT>:: do_hash
From cppreference.net
|
ヘッダーで定義
<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)
パブリックメンバー関数。最も派生したクラスの保護された仮想メンバー関数
do_hash
を呼び出します。
2)
文字シーケンス
[
beg
,
end
)
を、このロケールで照合順序が等価なすべての文字列に対して得られるハッシュ値と等しい整数値に変換します(
compare()
が
0
を返す場合)。照合順序が等価ではない2つの文字列について、それらのハッシュ値が等しくなる確率は非常に低く、
1.0
/
std::
numeric_limits
<
unsigned
long
>
::
max
(
)
に近づく必要があります。
目次 |
パラメータ
| beg | - | ハッシュ対象シーケンスの最初の文字へのポインタ |
| end | - | ハッシュ対象シーケンスの終端の次を指すポインタ |
戻り値
照合順序を尊重するハッシュ値。
注記
システム提供のロケールは通常、2つの文字列を等価として照合しません( compare() が 0 を返さない)もし basic_string::operator== が false を返す場合。しかし、ユーザーがインストールした std::collate ファセットは異なる照合規則を提供する可能性があります。例えば、同じUnicode正規化形式を持つ場合に文字列を等価として扱うことがあります。
例
ロケールを考慮した非順序コンテナの実装例を示します。
このコードを実行
#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'; }
出力例:
Bar Foo
関連項目
|
(C++11)
|
文字列のハッシュサポート
(クラステンプレートの特殊化) |