std::collate<CharT>::hash, std::collate<CharT>::do_hash
来自cppreference.com
| 在标头 <locale> 定义
|
||
| |
(1) | |
| |
(2) | |
1) 公开成员函数,调用最终派生类的受保护虚成员函数
do_hash。2) 转换字符序列
[beg, end) 为整数值,该值对所有此本地环境中校排等价(compare() 返回 0)的字符串相同。对于两个校排不等价的字符串,其散列相等的概率要非常小,近似 1.0/std::numeric_limits<unsigned long>::max()。参数
| beg | - | 指向要散列的序列中首字符的指针 |
| end | - | 要散列的序列的尾后一位置指针 |
返回值
相对于校排顺序的散列值。
注解
若 basic_string::operator== 返回 false,则系统提供的本地环照常校排两个字符串为不等价(compare() 不返回 0),但用户安装的 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) |
字符串的散列支持 (类模板特化) |