fork(5) download
  1.  
  2. #include <unordered_set>
  3.  
  4. struct Point {
  5. int X;
  6. int Y;
  7.  
  8. Point() : X(0), Y(0) {};
  9. Point(const int& x, const int& y) : X(x), Y(y) {};
  10. Point(const Point& other){
  11. X = other.X;
  12. Y = other.Y;
  13. };
  14.  
  15. Point& operator=(const Point& other) {
  16. X = other.X;
  17. Y = other.Y;
  18. return *this;
  19. };
  20.  
  21. bool operator==(const Point& other) {
  22. if (X == other.X && Y == other.Y)
  23. return true;
  24. return false;
  25. };
  26.  
  27. bool operator<(const Point& other) {
  28. if (X < other.X )
  29. return true;
  30. else if (X == other.X && Y == other.Y)
  31. return true;
  32.  
  33. return false;
  34. };
  35.  
  36. // this could be moved in to std::hash<Point>::operator()
  37. size_t operator()(const Point& pointToHash) const noexcept {
  38. size_t hash = pointToHash.X + 10 * pointToHash.Y;
  39. return hash;
  40. };
  41.  
  42. };
  43.  
  44. namespace std {
  45. template<> struct hash<Point>
  46. {
  47. std::size_t operator()(const Point& p) const noexcept
  48. {
  49. return p(p);
  50. }
  51. };
  52. }
  53.  
  54. int main()
  55. {
  56. // no need to specify the hasher if std::hash<Point> exists
  57. std::unordered_set<Point> p;
  58. return 0;
  59. }
Success #stdin #stdout 0s 4316KB
stdin
Standard input is empty
stdout
Standard output is empty