哈希类是默认可构造的,这意味着可以构造该对象而无需任何参数或初始化值。它用于获取传递给它的参数的哈希值。如果参数不变,则值也不变。
句法:
template struct hash;
创建对象的语法:
hash object-name
成员函数:该Hash类只有一个成员函数:
- 运算符():返回给定参数的哈希值。
下面是哈希类的实现:
例子:
#include
// functional header
// for hash class
#include
#include
#include
#include
using namespace std;
// To demonstrate String Hashing
void stringHashing()
{
// Get the string
// to get its hash value
string hashing1 = "Geeks";
// Instatiation of Object
hash mystdhash;
// Using operator() to get hash value
cout << "String hash values: "
<< mystdhash(hashing1)
<< endl;
}
// To demonstrate BITSET Hashing
void bitsetHashing()
{
// Get the BITSET
// to get its hash value
bitset<5> h_bitset("10101");
// Instatiation of Object
hash > hash_bitset;
// Using operator() to get hash value
cout << "\nBitset 10101 hash value: "
<< hash_bitset(h_bitset) << endl;
}
// To demonstrate Vector Hashing
void vectorHashing()
{
// Get the Vector
// to get its hash value
vector
h_vecbool{ true, false,
true, false };
// Instatiation of Object
hash > hash_vector_bool;
// Using operator() to get hash value
cout << "\nVector hash value: "
<< hash_vector_bool(h_vecbool)
<< endl;
}
// To demonstrate Char Hashing
void charHashing()
{
// Get the char
// to get its hash value
char ch = 'a';
// Instatiation of Object
hash hash_char;
// Using operator() to get hash value
cout << "\nChar hash values: "
<< hash_char(ch)
<< endl;
}
// Driver Code
int main()
{
stringHashing();
bitsetHashing();
vectorHashing();
charHashing();
}
输出:
String hash values: 4457761756728957899
Bitset 10101 hash value: 17123654466142159564
Vector hash value: 16935082123893034051
Char hash values: 97
参考: http : //www.cplusplus.com/reference/functional/hash/
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。