fork(1) download
  1. #include <iostream>
  2. #include <map>
  3. #include <tuple>
  4. #include <vector>
  5.  
  6. struct Point2f
  7. {
  8. Point2f(double x, double y) : x(x), y(y) {}
  9.  
  10. double x;
  11. double y;
  12. };
  13.  
  14.  
  15. std::ostream& operator << (std::ostream& os, const Point2f& pt)
  16. {
  17. return os << "[" << pt.x << ", " << pt.y << "]";
  18. }
  19.  
  20. bool operator < (const Point2f& lhs, const Point2f& rhs)
  21. {
  22. return std::tie(lhs.x, lhs.y) < std::tie(rhs.x, rhs.y);
  23. }
  24.  
  25. int main()
  26. {
  27. std::vector<Point2f> points_1, points_2;
  28.  
  29. points_1.push_back(Point2f(1.0, 2.0));
  30. points_1.push_back(Point2f(2.0, 2.0));
  31. points_1.push_back(Point2f(3.0, 2.0));
  32. points_1.push_back(Point2f(1.0, 2.0));
  33. points_1.push_back(Point2f(2.0, 2.0));
  34. points_1.push_back(Point2f(3.0, 2.0));
  35. points_1.push_back(Point2f(1.0, 2.0));
  36.  
  37. points_2.push_back(Point2f(1.0, 1.0));
  38. points_2.push_back(Point2f(1.0, 1.0));
  39. points_2.push_back(Point2f(1.0, 2.0));
  40. points_2.push_back(Point2f(1.0, 1.0));
  41. points_2.push_back(Point2f(1.0, 1.0));
  42. points_2.push_back(Point2f(1.0, 1.0));
  43. points_2.push_back(Point2f(1.0, 1.0));
  44.  
  45. std::vector<std::pair<Point2f, Point2f>> point_pairs;
  46. for (size_t i = 0; i < points_1.size(); i++) {
  47. std::cout << points_1[i] << " " << points_2[i] << std::endl;
  48. point_pairs.push_back(std::make_pair(points_1[i], points_2[i]));
  49. }
  50.  
  51. std::cout << "==========\n";
  52.  
  53. std::map<std::pair<Point2f, Point2f>, int> counts;
  54.  
  55. for (const auto& p : point_pairs) {
  56. ++counts[p];
  57. }
  58.  
  59. for (const auto& p : counts) {
  60. const auto& p1 = p.first.first;
  61. const auto& p2 = p.first.second;
  62. int count = p.second;
  63. std::cout << p1 << " " << p2 << " - " << count << " Occurrence(s)\n";
  64. }
  65.  
  66. }
  67.  
Success #stdin #stdout 0s 16056KB
stdin
Standard input is empty
stdout
[1, 2] [1, 1]
[2, 2] [1, 1]
[3, 2] [1, 2]
[1, 2] [1, 1]
[2, 2] [1, 1]
[3, 2] [1, 1]
[1, 2] [1, 1]
==========
[1, 2] [1, 1] - 3 Occurrence(s)
[2, 2] [1, 1] - 2 Occurrence(s)
[3, 2] [1, 1] - 1 Occurrence(s)
[3, 2] [1, 2] - 1 Occurrence(s)