Skip to content

Instantly share code, notes, and snippets.

@airglow923
Created October 30, 2021 19:13
Show Gist options
  • Save airglow923/357e1ecfe8d9d65cfb7307cd15e3cac4 to your computer and use it in GitHub Desktop.
Save airglow923/357e1ecfe8d9d65cfb7307cd15e3cac4 to your computer and use it in GitHub Desktop.
Custom hash for unordered_set
#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