Skip to content

Instantly share code, notes, and snippets.

@tabe
Last active May 16, 2016 09:12
Show Gist options
  • Save tabe/de1f3a6453d68ee52f6327d39c6b1898 to your computer and use it in GitHub Desktop.
Save tabe/de1f3a6453d68ee52f6327d39c6b1898 to your computer and use it in GitHub Desktop.
hash_func & key_equal of std::unordered_map
#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