std::hash
From Cppreference
C++ Standard Library | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Utilities library | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::hash | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <functional>
|
||
template< class Key >
struct hash; |
(since C++11) | |
The hash template defines a hash function object such that if two objects K and M of the same type Key compare equal, then std::hash(K)==std::hash(M), although the reverse is not necessarily true.
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.
Contents |
[edit] Member types
argument_type | Key |
result_type | std::size_t |
[edit] Member functions
constructs a hash function object (public member function) |
|
calculate the hash of the argument (public member function) |
[edit] Standard specializations for basic types
Defined in header <functional>
|
||
template<> struct hash<bool>;
template<> struct hash<char>; |
||
[edit] Standard specializations for library types
(C++11)
(C++11) (C++11) (C++11) |
hash support for strings (class template specialization) |
(C++11)
|
hash support for std::error_code (class template specialization) |
(C++11)
|
hash support for std::bitset (class template specialization) |
(C++11)
|
hash support for std::unique_ptr (class template specialization) |
(C++11)
|
hash support for std::shared_ptr (class template specialization) |
(C++11)
|
hash support for std::type_index (class template specialization) |
(C++11)
|
hash support for std::vector<bool> (class template specialization) |
(C++11)
|
hash support for std::thread::id (class template specialization) |
[edit] Example
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. requires also specialization of std::equal_to.
#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= "Peter"; std::string s2= "Pan"; 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