fork(2) download
  1. #include <unordered_set>
  2. #include <iostream>
  3.  
  4. struct Point {
  5. int X, Y;
  6.  
  7. Point() : X(0), Y(0) {};
  8. Point(const int x, const int y) : X(x), Y(y) {};
  9. };
  10.  
  11. int main() {
  12. auto hash = [](const Point& p) { return std::hash<int>()(p.X) * 31 + std::hash<int>()(p.Y); };
  13. auto equal = [](const Point& p1, const Point& p2) { return p1.X == p2.X && p1.Y == p2.Y; };
  14. std::unordered_set<Point, decltype(hash), decltype(equal)> mySet(8, hash, equal);
  15.  
  16. Point a, b(1, 2), c(b), d(3, 4);
  17.  
  18. mySet.emplace(a);
  19. mySet.emplace(b);
  20. mySet.emplace(c); // Insertion fails; element already exists.
  21. c = d;
  22. mySet.emplace(c);
  23. mySet.emplace(1, 1);
  24.  
  25. for (auto const& pt : mySet) {
  26. std::cout << "(" << pt.X << ", " << pt.Y << ")" << std::endl;
  27. }
  28.  
  29. return 0;
  30. }
Success #stdin #stdout 0s 4548KB
stdin
Standard input is empty
stdout
(1, 1)
(3, 4)
(1, 2)
(0, 0)