#include #include #include #include #include #include using namespace std; class Product { public: Product() : name{}, weight{}, price{} {} Product(string const& n, double w, double p) : name{n}, weight{w}, price{p} {} double compare_price() const // kr / kg { return price / weight; } bool operator<(Product const& rhs) const { if (compare_price() < rhs.compare_price()) { return true; } else if ( compare_price() > rhs.compare_price() ) { return false; } return name < rhs.name; } /* return make_tuple( compare_price(), name ) < make_tuple( rhs.compare_price(), name); } */ friend istream& operator>>(istream& is, Product& p) { string name; string price; string weight; getline( is, name, ';'); getline( is, weight, ';'); getline( is, price, ';'); istringstream nis{name}; nis >> p.name; istringstream wis{weight}; wis >> p.weight; istringstream pis{price}; pis >> p.price; return is; } friend ostream& operator<<(ostream& os, Product const& p) { return os << p.name << ", " << p.compare_price() << " kr/kg"; } private: string name; double weight; // kg double price; // kr }; int main() { cout << setprecision(2) << fixed; vector pv(3); int n{1}; for ( Product& i : pv ) { cout << "Mata in produkt " << n++ << ": "; cin >> i; } //sort(pv.begin(), pv.end()); if ( pv[2] < pv[1] ) { swap(pv[2], pv[1]); } if ( pv[1] < pv[0] ) { swap(pv[1], pv[0]); } if ( pv[2] < pv[1] ) { swap(pv[2], pv[1]); } cout << "\nProdukterna ordnade med mest prisvärd produkt först:" << endl; for ( Product const& i : pv ) { cout << i << endl; } return 0; }