Загрузка данных


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

class Book {
private:
    std::string title;
    std::string author;
    int year;

public:
    Book(std::string t, std::string a, int y)
        : title(std::move(t)), author(std::move(a)), year(y) {}

    const std::string& getTitle() const { return title; }
    const std::string& getAuthor() const { return author; }
    int getYear() const { return year; }

    friend std::ostream& operator<<(std::ostream& os, const Book& b) {
        os << "\"" << b.title << "\" by " << b.author << " (" << b.year << ")";
        return os;
    }
};

int main() {
    std::vector<Book> library;

    // Добавление книг (аналог add_book)
    library.emplace_back("The C Programming Language", "K&R", 1978);
    library.emplace_back("Clean Code", "Robert C. Martin", 2008);
    library.emplace_back("Design Patterns", "GoF", 1994);

    std::cout << "Before sorting:\n";
    for (const auto& book : library) {
        std::cout << book << '\n';
    }

    // Сортировка по году (лямбда)
    std::sort(library.begin(), library.end(),
              [](const Book& a, const Book& b) { return a.getYear() < b.getYear(); });

    std::cout << "\nSorted by year:\n";
    for (const auto& book : library) {
        std::cout << book << '\n';
    }

    return 0;
}