Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <tuple>
- #include <unordered_set>
- using namespace std;
- template <class T>
- inline size_t hash_combine(std::size_t seed, T const& v) {
- return seed ^ std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
- }
- namespace std {
- template <typename... Ts>
- class hash<tuple<Ts...>> {
- public:
- size_t operator()(const tuple<Ts...>& t) const { return impl<0>(t); }
- template <size_t Index>
- typename enable_if<Index == tuple_size<tuple<Ts...>>::value, size_t>::type
- impl(const tuple<Ts...>& t) const {
- return 0;
- }
- template <size_t Index>
- typename enable_if<Index != tuple_size<tuple<Ts...>>::value, size_t>::type
- impl(const tuple<Ts...>& t) const {
- return hash_combine(impl<Index + 1>(t), get<Index>(t));
- }
- };
- }
- int main() {
- auto t = make_tuple(1, 2.0, string("abc"));
- cout << hash<decltype(t)>()(t) << endl;
- cout << hash_combine(hash_combine(hash_combine(0, string("abc")), 2.0), 1) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement