1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <iostream>
#include <string>
#include <map>
#include <vector>
using std::string;
using std::vector;
struct Book {
string title;
};
struct Student {
int id;
string first;
string last;
};
bool operator<(const Student& a, const Student& b)
{
return a.id < b.id;
}
int main()
{
std::map<Student, vector<Book>> student_books;
student_books.insert( std::pair<Student, vector<Book>>(
{1234, "Jane", "Doe"},
vector<Book>{ {"War and Peace"}, {"The Hobbit"} }
));
student_books.insert( std::pair<Student, vector<Book>>(
{456, "John", "Smith"},
vector<Book>{ {"Anna Karenina"} }
));
student_books.insert( std::pair<Student, vector<Book>>(
{456, "Billy", "the Kid"},
vector<Book>{ {"An Elementary Treatise on Electricity"}, {"Other books I haven't read"} }
));
for (const auto& element : student_books)
{
std::cout << element.first.id << "\n";
for (const auto& book : element.second)
{
std::cout << " " << book.title;
}
std::cout << '\n';
}
}
|