mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
Add Java and C++ code for the section hash algorithm (#560)
This commit is contained in:
29
codes/cpp/chapter_hashing/built_in_hash.cpp
Normal file
29
codes/cpp/chapter_hashing/built_in_hash.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* File: built_in_hash.cpp
|
||||
* Created Time: 2023-06-21
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int num = 3;
|
||||
size_t hashNum = hash<int>()(num);
|
||||
cout << "整数 " << num << " 的哈希值为 " << hashNum << "\n";
|
||||
|
||||
bool bol = true;
|
||||
size_t hashBol = hash<bool>()(bol);
|
||||
cout << "布尔量 " << bol << " 的哈希值为 " << hashBol << "\n";
|
||||
|
||||
double dec = 3.14159;
|
||||
size_t hashDec = hash<double>()(dec);
|
||||
cout << "小数 " << dec << " 的哈希值为 " << hashDec << "\n";
|
||||
|
||||
string str = "Hello 算法";
|
||||
size_t hashStr = hash<string>()(str);
|
||||
cout << "字符串 " << str << " 的哈希值为 " << hashStr << "\n";
|
||||
|
||||
// 在 C++ 中,内置 std:hash() 仅提供基本数据类型的哈希值计算
|
||||
// 数组、对象的哈希值计算需要自行实现
|
||||
}
|
||||
Reference in New Issue
Block a user