-
-
Save tabe/de1f3a6453d68ee52f6327d39c6b1898 to your computer and use it in GitHub Desktop.
hash_func & key_equal of std::unordered_map
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 <cstdio> | |
#include <cstdlib> | |
#include <iostream> | |
#include <string> | |
#include <unordered_map> | |
struct Eq { | |
bool operator()(int i, int j) const | |
{ | |
return std::abs(i) == std::abs(j); | |
} | |
}; | |
int main() | |
{ | |
std::unordered_map<int, std::string, std::hash<int>, Eq> m = { | |
{1, "one"}, | |
{2, "two"}, | |
{3, "thee"} | |
}; | |
for (auto i : {-1, -2, -3}) { | |
auto it = m.find(i); | |
if (it != m.end()) | |
std::cout << "found: " << it->second << std::endl; | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment