首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

do_hash

Defined in header <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%29公共成员函数,调用受保护的虚拟成员函数do_hash最派生的类。

2%29转换字符序列[beg, end)的整数值,该整数值等于为在此区域设置%28中排序等价的所有字符串获得的散列值。compare()回报​0​29%。对于两个没有排序等价的字符串,它们的散列相等的概率应该非常小,接近。1.0/std::numeric_limits<unsignedlong>::max()...

参数

beg

-

pointer to the first character in the sequence to hash

end

-

one past the end pointer for the sequence to hash

返回值

尊重排序规则顺序的散列值。

系统提供的区域设置通常不会将两个字符串作为等效的%28进行排序。compare()不回来​0​%29basic_string::operator==回报false,但是用户安装了std::collatefacet可能提供不同的排序规则,例如,如果字符串具有相同的Unicode规范化形式,它可以将它们视为等效的。

演示识别区域设置的无序容器。

二次

代码语言:javascript
复制
#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';
}

二次

可能的产出:

二次

代码语言:javascript
复制
Bar Foo

二次

另见

std::hash<std::string>std::hash<std::u16string>std::hash<std::u32string>std::hash<std::wstring> (C++11)(C++11)(C++11)(C++11)

hash support for strings (class template specialization)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券