Book Image

Learning Boost C++ Libraries

By : Arindam Mukherjee
Book Image

Learning Boost C++ Libraries

By: Arindam Mukherjee

Overview of this book

Table of Contents (19 chapters)
Learning Boost C++ Libraries
About the Author
About the Reviewers

Higher order programming with Boost

Consider a type Book with three string fields: the ISBN, title, and author (for our purposes, assume that there is only one author). Here is how we can choose to define this type:

 1 struct Book
 2 {
 3   Book(const std::string& id,
 4        const std::string& name,
 5        const std::string& auth)
 6         : isbn(id), title(name), author(auth)
 7   {}
 9   std::string isbn;
10   std::string title;
11   std::string author;
12 };
14 bool operator< (const Book& lhs, const Book& rhs)
12 {  return lhs.isbn < rhs.isbn;  }

It is a struct with three fields and a constructor that initializes these three fields. The isbn field uniquely identifies the book and therefore is used to define an ordering of Book objects, using the overloaded operator< (line 14).

Now imagine that we have a list of these Book objects in a std::vector, and we want to sort these books. Thanks to the overloaded operator<, we can easily sort them using...