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

std::hash

Defined in header <string>

template<> struct hash<std::string>; template<> struct hash<std::wstring>; template<> struct hash<std::u16string>; template<> struct hash<std::u32string>;

(since C++11)

模板的专门化std::hash对于各种字符串类,允许用户获取字符串的散列。

These hashes equal the hashes of corresponding std::basic_string_view classes: If S is one of these string types, SV is the corresponding string view type, and s is an object of type S, then std::hash<S>()(s) == std::hash<SV>()(SV(s)).

(since C++17)

下面的代码显示了字符串上使用的哈希函数的一个可能输出:

二次

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <functional>
 
int main()
{
    std::string s = "Stand back! I've got jimmies!";
    std::hash<std::string> hash_fn;
 
    size_t hash = hash_fn(s);
 
    std::cout << hash << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
325378910

二次

另见

hash (C++11)

hash function object (class template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券