Source of multiset03.cpp


  1: //multiset03.cpp

  3: #include <iostream>
  4: #include <set>
  5: #include <string>
  6: using namespace std;


  9: //This class stores info about a book, indexed ("keyed") by author.
 10: class Book
 11: {
 12: public:
 13:     Book() { title = author = publisher = date = ""; }
 14:     Book(string a) { author = a; title = publisher = date = ""; }
 15:     Book(string t, string a, string p, string d)
 16:     { title = t; author = a; publisher = p; date = d; }
 17:     
 18:     string Author() { return author; }
 19:     void GetInfo(string &t, string &a, string &p, string &d)
 20:     { t = title; a = author; p = publisher; d = date; }
 21: private:
 22:     string author;
 23:     string title;
 24:     string publisher;
 25:     string date;
 26: };

 28: bool operator<(Book b1, Book b2) 
 29: {
 30:     return b1.Author() < b2.Author();
 31: }

 33: ostream &operator<<(ostream &os, Book &b)
 34: {
 35:     string t, a, p, d;
 36:     b.GetInfo(t, a, p, d);
 37:     os << a << endl;
 38:     os << t << endl;
 39:     os << p << endl;
 40:     os << d << endl;
 41:     return os;
 42: } 


 45: int main()
 46: {

 48:     cout << "\nThis program stores book objects in a multiset and then "
 49:         "\npermits the user to retrieve all books by any author.";
 50:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 52:     multiset<Book> bookList;
 53:     string a;
 54:     
 55:     //Initialize the multiset of books
 56:     bookList.insert(Book("C++: The Complete Reference",
 57:         "Schildt", "Osborne/McGraw-Hill",
 58:         "1998"));
 59:     bookList.insert(Book("C++ From The Ground Up",
 60:         "Schildt", "Osborne/McGraw-Hill",
 61:         "1998"));
 62:     bookList.insert(Book("Teach Yourself C++",
 63:         "Schildt", "Osborne/McGraw-Hill",
 64:         "1997"));
 65:     bookList.insert(Book("Executive Orders",
 66:         "Clancy", "Putnam",
 67:         "1996"));
 68:     bookList.insert(Book("The Hunt for Red October",
 69:         "Clancy", "Berkeley",
 70:         "1985"));
 71:     bookList.insert(Book("Red Storm Rising",
 72:         "Clancy", "Berkeley",
 73:         "1987"));
 74:     bookList.insert(Book("Sphere",
 75:         "Crichton", "Ballantine",
 76:         "1987"));
 77:     bookList.insert(Book("Jurassic Park",
 78:         "Crichton", "Ballantine",
 79:         "1990"));
 80:     bookList.insert(Book("The Lost World",
 81:         "Crichton", "Knopf",
 82:         "1995"));
 83:     
 84:     cout << "\nHere are all the books in our \"database\":\n";
 85:     multiset<Book>::iterator p = bookList.begin();
 86:     do
 87:     {
 88:         cout << endl << *p++;
 89:         cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');
 90:     }
 91:     while (p != bookList.end());
 92:     cout << endl;

 94:     cout << "\n\n\nHere are all books by Clancy: \n\n";
 95:     p = bookList.find(Book("Clancy"));
 96:     for (int i=0; i<(int)bookList.count(Book("Clancy")); i++)
 97:     { 
 98:         cout << *p << endl;
 99:         p++;
100:     }
101:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

103:     cout << "\n\n\nHere are all books by Crichton: \n\n";
104:     pair<multiset<Book>::iterator, multiset<Book>::iterator> range;
105:     range = bookList.equal_range(Book("Crichton"));
106:     for (p = range.first; p != range.second; p++) 
107:         cout << *p << endl;
108:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n');

110:     //Find all books by a given author:
111:     cout << "\nFor a list of books by any author, "
112:             "enter name of that author: ";
113:     cin >> a;  cin.ignore(80, '\n');  cout << endl;
114:     p = bookList.find(Book(a));
115:     if (p != bookList.end())
116:     {
117:         do
118:         {
119:             cout << *p << endl;
120:             p++;
121:         }
122:         while (p != bookList.upper_bound(a));
123:     }
124:     else
125:         cout << "We don't seem to have any books by that author.\n";
126:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
127: }