fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. struct Point
  6. {
  7. float x, y;
  8. Point(float x, float y) : x(x), y(y) {}
  9. };
  10.  
  11. int main()
  12. {
  13. std::vector<Point> points = {
  14. Point(-1.0f, 1.0f),
  15. Point(1.0f, 1.0f),
  16. Point(-2.0f, 1.0f),
  17. Point(2.0f, 1.0f),
  18. Point(4.0f, 2.0f),
  19. Point(-4.0f, 2.0f),
  20. Point(-8.0f, 2.0f),
  21. Point(8.0f, 2.0f),
  22. Point(0.0f, 3.0f)
  23. };
  24.  
  25. std::sort(
  26. points.begin(), points.end(),
  27. [](const Point &lhs, const Point &rhs) {
  28. if (lhs.x < rhs.x)
  29. return true;
  30. return false;
  31. }
  32. );
  33. std::stable_sort(
  34. points.begin(), points.end(),
  35. [](const Point &lhs, const Point &rhs) -> bool {
  36. if (lhs.y < rhs.y)
  37. return true;
  38. return false;
  39. }
  40. );
  41.  
  42. for (auto &p : points) {
  43. std::cout << "(" << p.x << ", " << p.y << "), ";
  44. }
  45. }
Success #stdin #stdout 0s 15248KB
stdin
Standard input is empty
stdout
(-2, 1), (-1, 1), (1, 1), (2, 1), (-8, 2), (-4, 2), (4, 2), (8, 2), (0, 3),