fork download
  1. #include <vector>
  2. using std::vector;
  3.  
  4. #include <algorithm>
  5. using std::sort;
  6.  
  7. #include <iostream>
  8. using std::cout;
  9.  
  10. class CartesianPoint
  11. {
  12. private:
  13. int x;
  14. int y;
  15.  
  16. public:
  17. int getX() const { return x; }
  18. int getY() const { return y; }
  19. CartesianPoint(int x, int y) : x(x), y(y) { }
  20. bool operator< (const CartesianPoint& that) const { return x < that.x; }
  21.  
  22. };
  23.  
  24. int main()
  25. {
  26. vector<CartesianPoint> myvector { {10, 13}, {32, 1}, {5, 29}, {12, 6}, {21, 100} };
  27.  
  28. auto cmpFunc = [](const auto& p1, const auto& p2) { return p1 < p2; };
  29. sort(myvector.begin(), myvector.end(), cmpFunc);
  30.  
  31. cout << "myvector contains:";
  32.  
  33. for (const auto& point : myvector)
  34. cout << ' ' << '(' << point.getX() << ',' << point.getY() << ')';
  35.  
  36. cout << '\n';
  37. }
  38.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
myvector contains: (5,29) (10,13) (12,6) (21,100) (32,1)