Source of multiset05.cpp


  1: //multiset05.cpp

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

  7: int main()
  8: {
  9:     cout << "\nThis program displays first a set, then a multiset, of "
 10:         "positive integer values.\nIn each case, the user is permitted "
 11:         "to enter values of the user's choice and\nthe program provides "
 12:         "the lower and upper bound of each value entered, relative\nto "
 13:         "the set of values, or the multiset of values, as the case may be.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 16:     int a_s[] ={2, 4, 6, 8, 10};
 17:     set<int> s(a_s, a_s+5);
 18:     cout << "\nHere are the values in the set:\n";
 19:     ostream_iterator<int> os(cout, " "); 
 20:     copy(s.begin(), s.end(), os);  cout << endl;
 21:     int value;
 22:     cout << "\nEnter a value to see its lower and upper bound "
 23:         "\nin the above set (or end-of-file to quit): ";
 24:     while (cin >> value)
 25:     {
 26:         cin.ignore(80, '\n');
 27:         set<int>::iterator lower = s.lower_bound(value);
 28:         set<int>::iterator upper = s.upper_bound(value);
 29:         cout << "\nLower bound of " << value << " = ";
 30:         if (lower == s.end()) cout << "the end of the range";
 31:         else cout << *lower;
 32:         cout << "\nUpper bound of " << value << " = ";
 33:         if (upper == s.end()) cout << "the end of the range";
 34:         else cout << *upper;
 35:         cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 36:         cout << "\nEnter a value to see its lower and upper bound "
 37:             "\nin the above sequence (or end-of-file to quit): ";
 38:     }

 40:     cin.clear();
 41:     cout << "\nNow we do the same for a multiset of values.";
 42:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 44:     int a_ms[] = {2, 2, 2, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 10};
 45:     multiset<int> ms(a_ms, a_ms+15);
 46:     cout << "\nHere are the values in the multiset:\n";
 47:     copy(ms.begin(), ms.end(), os);  cout << endl;
 48:     cout << "\nEnter a value to see its lower and upper bound "
 49:         "\nin the above multiset (or end-of-file to quit): ";
 50:     while (cin >> value)
 51:     {
 52:         cin.ignore(80, '\n');
 53:         multiset<int>::iterator lower_ms = ms.lower_bound(value);
 54:         multiset<int>::iterator upper_ms = ms.upper_bound(value);
 55:         cout << "\nLower bound of " << value << " = ";
 56:         if (lower_ms == ms.end()) cout << "the end of the range";
 57:         else cout << *lower_ms;
 58:         cout << "\nUpper bound of " << value << " = ";
 59:         if (upper_ms == ms.end()) cout << "the end of the range";
 60:         else cout << *upper_ms;
 61:         cout << "\nPress Enter to continue .. ";  cin.ignore(80, '\n');
 62:         cout << "\nEnter a value to see its lower and upper bound "
 63:             "\nin the above sequence (or end-of-file to quit): ";
 64:     }
 65: }