Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- class Product {
- public:
- std::string name;
- std::string type;
- double price;
- Product(const std::string& name, const std::string& type, double price)
- : name(name), type(type), price(price) {}
- };
- bool compareByPrice(const Product& p1, const Product& p2) {
- return p1.price < p2.price;
- }
- int main() {
- std::vector<Product> products;
- // Add some sample products
- products.push_back(Product("Laptop", "Tech", 999.99));
- products.push_back(Product("Smartphone", "Tech", 799.99));
- products.push_back(Product("Headphones", "Tech", 199.99));
- products.push_back(Product("TV", "Tech", 1499.99));
- // Sort the products by price
- std::sort(products.begin(), products.end(), compareByPrice);
- // Print the sorted products
- std::cout << "Sorted products by price:" << std::endl;
- for (const auto& product : products) {
- std::cout << product.name << " - $" << product.price << std::endl;
- }
- // Search for a product by name
- std::string searchName;
- std::cout << "Enter the name of the product to search: ";
- std::cin >> searchName;
- // Find the product by name
- bool found = false;
- for (const auto& product : products) {
- if (product.name == searchName) {
- std::cout << "Price of " << product.name << ": $" << product.price << std::endl;
- found = true;
- break;
- }
- }
- if (!found) {
- std::cout << "Product is not available" << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement