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


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

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

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

    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;

    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& b : library)
        std::cout << b << '\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& b : library)
        std::cout << b << '\n';

    return 0;
}