#include #include #include #include #include #include struct Product { std::string name { }; double price { }; int stock { }; }; namespace std { std::istream& operator>>(std::istream& is, Product& product) { return is >> product.name >> product.price >> product.stock; } std::ostream& operator<<(std::ostream& os, Product const& product) { return os << product.name << " (" << product.price << " SEK)"; } } int main() { std::ifstream ifs { "products.txt" }; std::vector products { std::istream_iterator{ ifs }, std::istream_iterator{ } }; std::sort(std::begin(products), std::end(products), [](auto&& p1, auto&& p2) { return p1.price > p2.price; }); auto it = std::stable_partition(std::begin(products), std::end(products), [](Product const& p) { return p.stock > 0; }); std::cout << "Total value of stock: " << std::accumulate(std::begin(products), it, 0.0, [](double total, Product const& p) { return total + (p.price * p.stock); }) << std::endl; std::ostream_iterator oit { std::cout, "\n" }; std::cout << "Products in-stock: " << std::endl; std::copy(std::begin(products), it, oit); std::cout << "Products out-of-stock: " << std::endl; std::copy(it, std::end(products), oit); }