fork(3) download
  1. #include <iostream>
  2. #include <unordered_set>
  3.  
  4. struct test
  5. {
  6. std::size_t hashCode() const
  7. {
  8. return 0;//insert your has routine
  9. }
  10. };
  11. //helper class
  12. struct hashable
  13. {
  14. hashable():value(0){}
  15. template<typename T>
  16. hashable(const T& t):value(t.hashCode())
  17. {}
  18. template<typename T>
  19. std::size_t operator()(const T& t) const
  20. {
  21. return t.hashCode();
  22. }
  23.  
  24. std::size_t value;
  25. };
  26.  
  27.  
  28. //hash specialization of hashable
  29. namespace std {
  30. template<>
  31. struct hash<hashable>
  32. {
  33. typedef hashable argument_type;
  34. typedef std::size_t result_type;
  35. result_type operator()(const argument_type& b) const {
  36. return b.value;
  37. }
  38. };
  39. }
  40. //helper alias so you dont have to specify the hash each time.
  41. template<typename T, typename hash = hashable>
  42. using unordered_set = std::unordered_set<T,hash>;
  43.  
  44. int main(int argc, char** argv)
  45. {
  46. unordered_set<test> s;
  47. test t;
  48. std::cout<<std::hash<hashable>{}(t)<<std::endl;
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
0