class template
std::hash
<functional>
template <class T> struct hash;
Default hash function object class
Unary function object class that defines the default hash function used by the standard library.
The functional call returns a hash value of its argument: A hash value is a value that depends solely on its argument, returning always the same value for the same argument. The value returned shall have a small likelihood of being the same as the one returned for a different argument (with chances of collision approaching 1/numeric_limits<size_t>::max).
Other function object types can be used as Hash for unordered containers: They need to be copy constructible and destructible and provide semantics compatible with this class.
The default hash is a template class that is not defined for the general case, only for type-specific specializations:
Member functions
- operator()
- Returns a hash value for its argument, as a value of type size_t.
size_t is an unsigned integral type.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
// hash example
#include <iostream>
#include <functional>
#include <string>
int main ()
{
char nts1[] = "Test";
char nts2[] = "Test";
std::string str1 (nts1);
std::string str2 (nts2);
std::hash<char*> ptr_hash;
std::hash<std::string> str_hash;
std::cout << "same hashes:\n" << std::boolalpha;
std::cout << "nts1 and nts2: " << (ptr_hash(nts1)==ptr_hash(nts2)) << '\n';
std::cout << "str1 and str2: " << (str_hash(str1)==str_hash(str2)) << '\n';
return 0;
}
|
Output:
same hashes:
nts1 and nts2: false
str1 and str2: true
|