std::hash class in C++ STL
The hash class is default constructible, which means that one can construct this object without any arguments or initialization values. It is used to get the hash value of the argument that is being passed to it. If the argument doesn’t change, the value doesn’t change either.
Syntax:
template <class T> struct hash;
Syntax to create object:
hash<class template> object-name
Member functions: This Hash class only has one member function:
- operator(): It returns hashed value for given argument.
Below is the implementation of the hash class:
Example:
CPP
#include <bitset>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void stringHashing()
{
string hashing1 = "Geeks" ;
hash<string> mystdhash;
cout << "String hash values: "
<< mystdhash(hashing1)
<< endl;
}
void bitsetHashing()
{
bitset<5> h_bitset( "10101" );
hash<bitset<5> > hash_bitset;
cout << "\nBitset 10101 hash value: "
<< hash_bitset(h_bitset) << endl;
}
void vectorHashing()
{
vector< bool >
h_vecbool{ true , false ,
true , false };
hash<vector< bool > > hash_vector_bool;
cout << "\nVector<bool> hash value: "
<< hash_vector_bool(h_vecbool)
<< endl;
}
void charHashing()
{
char ch = 'a' ;
hash< char > hash_char;
cout << "\nChar hash values: "
<< hash_char(ch)
<< endl;
}
int main()
{
stringHashing();
bitsetHashing();
vectorHashing();
charHashing();
}
|
Output:
String hash values: 4457761756728957899
Bitset 10101 hash value: 17123654466142159564
Vector hash value: 16935082123893034051
Char hash values: 97
Reference: http://www.cplusplus.com/reference/functional/hash/
Last Updated :
24 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...