Created
October 30, 2021 19:13
-
-
Save airglow923/357e1ecfe8d9d65cfb7307cd15e3cac4 to your computer and use it in GitHub Desktop.
Custom hash for unordered_set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <concepts> // integral | |
#include <unordered_set> | |
#include <utility> // pair | |
template <std::integral First, std::integral Second> | |
struct std::hash<std::pair<First, Second>> { | |
using argument_type = std::pair<First, Second>; | |
using result_type = std::size_t; | |
auto | |
operator()(const std::pair<First, Second> &pair) const noexcept | |
-> std::size_t { | |
const std::size_t h1 = std::hash<First>()(pair.first); | |
const std::size_t h2 = std::hash<Second>()(pair.second); | |
return h1 ^ h2; | |
} | |
}; | |
auto main() -> int { | |
std::unordered_set<std::pair<int, int>> set; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment