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. };
  21.  
  22. static bool myfunction(const CartesianPoint& a, const CartesianPoint& b)
  23. {
  24. return a.getX() < b.getX();
  25. }
  26.  
  27. int main()
  28. {
  29. vector<CartesianPoint> myvector { {10, 13}, {32, 1}, {5, 29}, {12, 6}, {21, 100} };
  30.  
  31. sort(myvector.begin(), myvector.end(), myfunction);
  32.  
  33. cout << "myvector contains:";
  34.  
  35. for (auto& point : myvector)
  36. cout << ' ' << '(' << point.getX() << ',' << point.getY() << ')';
  37.  
  38. cout << '\n';
  39. }
  40.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
myvector contains: (5,29) (10,13) (12,6) (21,100) (32,1)