std::hash

来自cppreference.com

 
 
实用工具库
类型的支持 (basic types, RTTI, type traits)
动态内存管理
错误处理
程序实用工具
可变参数函数
日期和时间
函数对象
initializer_list(C++11)
bitset
hash(C++11)
关系运算符
Original:
Relational operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rel_ops::operator!=
rel_ops::operator>
rel_ops::operator<=
rel_ops::operator>=
双和元组
Original:
Pairs and tuples
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pair
tuple(C++11)
piecewise_construct_t(C++11)
piecewise_construct(C++11)
掉期,远期和移动
Original:
Swap, forward and move
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
swap
forward(C++11)
move(C++11)
move_if_noexcept(C++11)
declval(C++11)
 
 
Defined in header <functional>
template< class Key >
struct hash; // not defined
(C++11 起)
哈希模板定义一个函数对象,实现了散列函数。这个函数对象的实例定义一个operator()
Original:
The hash template defines a function object that implements a 散列函数. Instances of this function object define an operator() that:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
1。接受一个参数的类型Key.
Original:
1. Accepts a single parameter of type Key.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2。返回一个表示的哈希值的参数的值的类型size_t.
Original:
2. Returns a value of type size_t that represents the hash value of the parameter.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3。不会抛出异常时,称为.
Original:
3. Does not throw exceptions when called.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
4。两个参数k1k2是平等的,std::hash<Key>()(k1) == std::hash<Key>()(k2).
Original:
4. For two parameters k1 and k2 that are equal, std::hash<Key>()(k1) == std::hash<Key>()(k2).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
5。对于两个不同的参数k1k2是不相等的,概率std::hash<Key>()(k1) == std::hash<Key>()(k2)应非常小,接近1.0/std::numeric_limits<size_t>::max().
Original:
5. For two different parameters k1 and k2 that are not equal, the probability that std::hash<Key>()(k1) == std::hash<Key>()(k2) should be very small, approaching 1.0/std::numeric_limits<size_t>::max().
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
哈希模板是CopyConstructibleDestructible.
Original:
The hash template is both CopyConstructible and Destructible.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
的无序关联式容器std::unordered_setstd::unordered_multisetstd::unordered_mapstd::unordered_multimap使用专业的模板std::hash默认的散列函数.
Original:
The unordered associative containers std::unordered_set, std::unordered_multiset, std::unordered_map, std::unordered_multimap use specializations of the template std::hash as the default hash function.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目录

[编辑] 会员类型

argument_type Key
result_type std::size_t

[编辑] 成员函数

构造一个哈希函数对象
Original:
constructs a hash function object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数)
计算哈希的说法
Original:
calculate the hash of the argument
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(公共成员函数)

[编辑] 标准的专业基本类型

Defined in header <functional>
template<> struct hash<bool>;

template<> struct hash<char>;
template<> struct hash<signed char>;
template<> struct hash<unsigned char>;
template<> struct hash<char16_t>;
template<> struct hash<char32_t>;
template<> struct hash<wchar_t>;
template<> struct hash<short>;
template<> struct hash<unsigned short>;
template<> struct hash<int>;
template<> struct hash<unsigned int>;
template<> struct hash<long>;
template<> struct hash<long long>;
template<> struct hash<unsigned long>;
template<> struct hash<unsigned long long>;
template<> struct hash<float>;
template<> struct hash<double>;
template<> struct hash<long double>;

template< class T > struct hash<T*>;

[编辑] 标准库类型专业

字符串哈希支持
Original:
hash support for strings
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(类模板专业化) [edit]
std::error_code的哈希支持
Original:
hash support for std::error_code
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(类模板专业化) [edit]
std::bitset的哈希支持
Original:
hash support for std::bitset
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(类模板专业化) [edit]
std::unique_ptr的哈希支持
Original:
hash support for std::unique_ptr
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(类模板专业化) [edit]
std::shared_ptr的哈希支持
Original:
hash support for std::shared_ptr
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(类模板专业化) [edit]
std::type_index的哈希支持
Original:
hash support for std::type_index
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(类模板专业化) [edit]
std::vector<bool>的哈希支持
Original:
hash support for std::vector<bool>
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(类模板专业化)
std::thread::id的哈希支持
Original:
hash support for std::thread::id
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(类模板专业化)

[编辑] 的例子

演示std::string,已经有一个哈希专业化的类型的哈希计算.

Original:

Demonstrates the computation of a hash for std::string, a type that already has a hash specialization.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <iostream>
#include <functional>
#include <string>
 
int main()
{
    std::string str = "Meet the new boss...";
    std::hash<std::string> hash_fn;
    size_t str_hash = hash_fn(str);
 
    std::cout << str_hash << '\n';
}

Output:

391070135

演示如何创建一个用户定义的类型的哈希函数。以此为模板参数std::unordered_mapstd::unordered_set等也需要专业化的std::equal_to.

Original:

Demonstrates creation of a hash function for a user defined type. Using this as a template parameter for std::unordered_map, std::unordered_set, etc. also requires specialization of std::equal_to.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <iostream>
#include <functional>
#include <string>
 
struct S {
    std::string first_name;
    std::string last_name;
};
 
template<class T> class MyHash;
 
template<>
class MyHash<S> {
public:
    size_t operator()(const S &s) const
    {
        size_t h1 = std::hash<std::string>()(s.first_name);
        size_t h2 = std::hash<std::string>()(s.last_name);
        return h1 ^ (h2 << 1);
    }
};
 
int main()
{
    std::string s1 = "Hubert";
    std::string s2 = "Farnsworth";
    std::hash<std::string> h1;
 
    S n1;
    n1.first_name = s1;
    n1.last_name =  s2;
 
    std::cout << "hash(s1) = " << h1(s1) << "\n"
              << "hash(s2) = " << std::hash<std::string>()(s2) << "\n"
          << "hash(n1) = " << MyHash<S>()(n1) << "\n";
 
}

Output:

hash(s1) = 6119893563398376542
hash(s2) = 14988020022735710972
hash(n1) = 17649170831080298918

演示如何专门std::hash为用户定义的类型.

Original:

Demonstrates how to specialize std::hash for a user defined type.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <iostream>
#include <functional>
#include <string>
 
struct S {
    std::string first_name;
    std::string last_name;
};
 
namespace std {
template<>
class hash<S> {
public:
    size_t operator()(const S &s) const
    {
        size_t h1 = std::hash<std::string>()(s.first_name);
        size_t h2 = std::hash<std::string>()(s.last_name);
        return h1 ^ ( h2 << 1 );
    }
};
}
 
int main()
{
    S s;
    s.first_name = "Bender";
    s.last_name =  "Rodriguez";
    std::hash<S> hash_fn;
 
    std::cout << "hash(s) = " << hash_fn(s) << "\n";
}

Output:

hash(s) = 32902390710