fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. class Time {
  6. int hour;
  7. int minute;
  8. public:
  9. Time(int h, int m) : hour(h), minute(m) {
  10. }
  11.  
  12. int getHour() const { return hour; }
  13. int getMinute() const { return minute; }
  14.  
  15. friend bool operator<(const Time& a, const Time& b) {
  16. return a.getHour() < b.getHour();
  17. }
  18. };
  19.  
  20. class Times {
  21. std::vector<Time> t;
  22.  
  23. public:
  24. Times() {
  25. // Test data
  26. t.push_back(Time{10, 10});
  27. t.push_back(Time{9, 20});
  28. t.push_back(Time{8, 30});
  29.  
  30. std::sort(t.begin(), t.end());
  31. }
  32.  
  33. void display() {
  34. for (const auto& x : t) {
  35. std::cout << x.getHour() << ":" << x.getMinute() << '\n';
  36. }
  37. }
  38. };
  39.  
  40. int main() {
  41. Times times;
  42. times.display();
  43. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
8:30
9:20
10:10