fork download
  1. #include <algorithm>
  2. #include <cmath>
  3. #include <ios>
  4. #include <iterator>
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8.  
  9. namespace model {
  10.  
  11. struct GasStation
  12. {
  13. std::string name_;
  14. bool loyalty_card_;
  15. double price_; // Don't use doubles for currency
  16.  
  17. GasStation(std::string const& name, bool loyalty_card, double price)
  18. :name_(name)
  19. ,loyalty_card_(loyalty_card)
  20. ,price_(price)
  21. {
  22. }
  23.  
  24. double getprice(int kindOfGas) const
  25. {
  26. return price_ + kindOfGas;
  27. }
  28. };
  29.  
  30. std::ostream& operator <<(std::ostream& out, GasStation const& gs)
  31. {
  32. out << "{\n"
  33. << gs.name_ << '\n'
  34. << std::boolalpha << gs.loyalty_card_ << '\n'
  35. << "$" << gs.price_ << '\n'
  36. << "}\n";
  37. return out;
  38. }
  39.  
  40. struct StationVector
  41. {
  42. std::vector<GasStation> localStations;
  43.  
  44. struct SortByGrade
  45. {
  46. int kindOfGas_;
  47. double tolerance_;
  48.  
  49. SortByGrade(int kindOfGas)
  50. :kindOfGas_(kindOfGas)
  51. ,tolerance_(0.0001)
  52. {
  53. }
  54.  
  55. bool operator() (GasStation const& L, GasStation const& R) const
  56. {
  57. if (::fabs(L.getprice(kindOfGas_) - R.getprice(kindOfGas_)) > tolerance_)
  58. return L.getprice(kindOfGas_) < R.getprice(kindOfGas_);
  59. if (L.loyalty_card_ != R.loyalty_card_)
  60. return L.loyalty_card_ < R.loyalty_card_;
  61. return L.name_ < R.name_;
  62. }
  63. };
  64.  
  65. void sortByGrade(int kindOfGas)
  66. {
  67. std::sort(localStations.begin(), localStations.end(), SortByGrade(kindOfGas));
  68. }
  69.  
  70. void add(std::string const& name, bool loyalty_card, double price)
  71. {
  72. GasStation gs(name, loyalty_card, price);
  73. localStations.push_back(gs);
  74. }
  75.  
  76. void print(std::ostream& out)
  77. {
  78. std::copy(localStations.begin(), localStations.end(), std::ostream_iterator<GasStation>(out, "\n"));
  79. }
  80. };
  81.  
  82. }
  83.  
  84. int main()
  85. {
  86. model::StationVector sv;
  87. sv.add("Alex's Garage", true, 1.99);
  88. sv.add("Andy's Garage", true, 1.99);
  89. sv.add("Dave's Garage", false, 1.99);
  90. sv.add("Gary's Garage", false, 3.99);
  91. sv.add("Kent's Garage", true, 2.99);
  92. sv.add("Matt's Garage", false, 2.99);
  93. sv.add("Mike's Garage", true, 3.99);
  94. sv.add("Pete's Garage", true, 0.99);
  95.  
  96. std::cout << " ==== Before ==== " << std::endl;
  97. sv.print(std::cout);
  98.  
  99. sv.sortByGrade(0);
  100.  
  101. std::cout << " ==== After ==== " << std::endl;
  102. sv.print(std::cout);
  103.  
  104. sv.sortByGrade(0);
  105.  
  106. std::cout << " ==== After sorting again (are we stable?) ==== " << std::endl;
  107. sv.print(std::cout);
  108.  
  109. }
  110.  
Success #stdin #stdout 0s 2996KB
stdin
Standard input is empty
stdout
 ==== Before ==== 
{
Alex's Garage
true
$1.99
}

{
Andy's Garage
true
$1.99
}

{
Dave's Garage
false
$1.99
}

{
Gary's Garage
false
$3.99
}

{
Kent's Garage
true
$2.99
}

{
Matt's Garage
false
$2.99
}

{
Mike's Garage
true
$3.99
}

{
Pete's Garage
true
$0.99
}

 ==== After ==== 
{
Pete's Garage
true
$0.99
}

{
Dave's Garage
false
$1.99
}

{
Alex's Garage
true
$1.99
}

{
Andy's Garage
true
$1.99
}

{
Matt's Garage
false
$2.99
}

{
Kent's Garage
true
$2.99
}

{
Gary's Garage
false
$3.99
}

{
Mike's Garage
true
$3.99
}

 ==== After sorting again (are we stable?) ==== 
{
Pete's Garage
true
$0.99
}

{
Dave's Garage
false
$1.99
}

{
Alex's Garage
true
$1.99
}

{
Andy's Garage
true
$1.99
}

{
Matt's Garage
false
$2.99
}

{
Kent's Garage
true
$2.99
}

{
Gary's Garage
false
$3.99
}

{
Mike's Garage
true
$3.99
}