13-哈希

137 字
1 分钟
13-哈希

具体实现参考了如何使用 pair 作为 unordered_map 的 key? - 知乎

哈希#

#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using ll = long long;
namespace hash_util {
inline void hash_combine(ull& seed, ull value) {
seed ^= value + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
template <class T>
inline void hash_combine(ull& seed, const T& value) {
hash_combine(seed, hash<T>{}(value));
}

pair哈希#

struct pair_hash {
template <class T1, class T2>
ull operator()(const pair<T1, T2>& p) const {
ull seed = 0;
hash_combine(seed, p.first);
hash_combine(seed, p.second);
return seed;
}
};

tuple哈希#

struct tuple_hash {
template <class... Ts>
ull operator()(const tuple<Ts...>& t) const {
ull seed = 0;
apply([&seed](const auto&... args) {
(hash_combine(seed, args), ...);
}, t);
return seed;
}
};
}

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

13-哈希
https://skaco2.com/posts/01-algorithm/13-哈希/
作者
SKACO2
发布于
2026-04-09
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
SKACO2
Hello……
公告
欢迎来到我的博客!
音乐
封面

音乐

暂未播放

0:00 0:00
暂无歌词
分类
标签
站点统计
文章
53
分类
8
标签
54
总字数
58,255
运行时长
0
最后活动
0 天前

目录