#include #include #include #include #include struct Product { std::string name; double weight; double volume; double density() const { return weight / volume; } }; std::ostream& operator<<(std::ostream& os, Product product) { os << product.name << " [" << product.weight << " kg, " << product.volume << " m^3]"; return os; } std::istream& operator>>(std::istream& is, Product& product) { is >> product.name >> product.weight >> product.volume; return is; } int main(int argc, char** argv) { if (argc < 5) { std::cerr << "USAGE: " << argv[0] << " FILE LOWER UPPER THRESHOLD\n"; return 1; } std::pair tolerance { std::stod(argv[2]), std::stod(argv[3]) }; int threshold { std::stoi(argv[4]) }; std::ifstream ifs { argv[1] }; std::vector products { std::istream_iterator { ifs }, std::istream_iterator { } }; auto it = std::search_n(std::begin(products), std::end(products), threshold, tolerance, [](Product const& p, std::pair tolerance) { double density { p.density() }; return !(tolerance.first < density && density < tolerance.second); }); if (it != products.end()) { std::cout << "Found a faulty sequence after " << std::distance(std::begin(products), it) << " products:\n"; std::copy_n(it, threshold, std::ostream_iterator{std::cout, "\n"}); } else { std::cout << "No faults found" << std::endl; } }