#include <string>
#include <vector>
#include <iostream>
struct Author {
std::string name;
std::vector<std::string> books;
Author(std::string n) : name(std::move(n)) {}
void set_book(std::string book) {
books.emplace_back(std::move(book));
}
};
struct Library {
void generate();
std::vector<Author> authors;
};
void Library::generate() {
conststatic std::string author_names[] { "James Wilson", "Thomas George", "Peter Lammens", "Matthew Marker" };
conststatic std::vector<std::vector<std::string> > all_books {
{"Ready Ribbons", "Jumping Down", "Living In Life"}, // James Wilson
{"Just Shaved", "Ordering Dogs", "Partly Done"}, // Thomas George
{"Move Allowed", "No Way, No Play", "Kick Up"} // Peter Lammens
// No books for Matthew Marker!
};
// Sets the books for each author.
authors.clear();
for (size_t cnt {}; constauto& a : author_names)
if (authors.emplace_back(a); cnt < all_books.size())
for (constauto& b : all_books[cnt++])
authors.back().set_book(b);
}
int main() {
Library lib;
lib.generate();
for (constauto& l : lib.authors) {
std::cout << l.name << '\n';
for (constauto& b : l.books)
std::cout << b << '\n';
std::cout << '\n';
}
}
which displays:
James Wilson
Ready Ribbons
Jumping Down
Living In Life
Thomas George
Just Shaved
Ordering Dogs
Partly Done
Peter Lammens
Move Allowed
No Way, No Play
Kick Up
Matthew Marker