fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class Sales
  9. {
  10. public:
  11. string customer_name;
  12. string category;
  13. string aircraft;
  14. string day;
  15. string time;
  16. int week;
  17. int ticket_price;
  18. string payment;
  19.  
  20. public:
  21. Sales () {
  22. customer_name = "";
  23. category = "";
  24. aircraft = "";
  25. day = "";
  26. time = "";
  27. week = 0;
  28. ticket_price = 0;
  29. payment = "";
  30. }
  31.  
  32. Sales (string f_cat, string airc, string xday, string xtime, int xweek) {
  33. customer_name = "";
  34. category = f_cat;
  35. aircraft = airc;
  36. day = xday;
  37. time = xtime;
  38. week = xweek;
  39. ticket_price = 0;
  40. payment = "";
  41. }
  42.  
  43. friend ostream &operator<<(ostream &os, const Sales &s){
  44. os << s.customer_name << ":" << s.category;
  45. return os;
  46. }
  47.  
  48. bool operator<( const Sales& rhs) const{
  49. if (customer_name < rhs.customer_name ) return true;
  50. if (category < rhs.category ) return true;
  51. return false;
  52. }
  53.  
  54.  
  55. };
  56.  
  57.  
  58. using namespace std;
  59.  
  60.  
  61. int main(int argc, char**argv) {
  62.  
  63. vector <Sales> v;
  64. Sales a,b,c;
  65. a.customer_name = "HSBC"; a.category = "ASIA";
  66. b.customer_name = "BANK OF AMERICA"; b.category = "US";
  67. c.customer_name = "JP MORGAN CHASE"; c.category = "US";
  68.  
  69. v.push_back(a);
  70. v.push_back(b);
  71. v.push_back(c);
  72.  
  73. for( auto &i : v){ std::cout << i << endl; };
  74.  
  75. std::sort(v.begin(), v.end());
  76.  
  77. cout << endl;
  78.  
  79. for( auto &i : v){ std::cout << i << endl; };
  80.  
  81. return 0;
  82. }
Success #stdin #stdout 0s 3492KB
stdin
Standard input is empty
stdout
HSBC:ASIA
BANK OF AMERICA:US
JP MORGAN CHASE:US

BANK OF AMERICA:US
HSBC:ASIA
JP MORGAN CHASE:US