Source of set15.cpp


  1: //set15.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the equal_range() member function "
 11:         "for sets, which\nreturns a pair of iterators pointing at the upper "
 12:         "and lower bounds of its\nargument in the calling set of values.";
 13:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 15:     int a[] ={2, 4, 6, 8, 10};
 16:     set<int> s(a, a+5);

 18:     cout << "\nHere are the values in our set: ";
 19:     set<int>::iterator p = s.begin();
 20:     while (p != s.end()) cout << *p++ << " ";

 22:     pair<set<int>::iterator, set<int>::iterator> lower_upper;

 24:     lower_upper = s.equal_range(0);
 25:     if (lower_upper.first == s.end())
 26:         cout << "\nThe lower bound of 0 is at the end of the range.";
 27:     else
 28:         cout << "\nThe lower bound of 0 is " << *lower_upper.first << ".";
 29:     if (lower_upper.second == s.end())
 30:         cout << "\nThe upper bound of 0 is at the end of the range.";
 31:     else
 32:         cout << "\nThe upper bound of 0 is " << *lower_upper.first << ".";

 34:     lower_upper = s.equal_range(2);
 35:     if (lower_upper.first == s.end())
 36:         cout << "\nThe lower bound of 2 is at the end of the range.";
 37:     else
 38:         cout << "\nThe lower bound of 2 is " << *lower_upper.first << ".";
 39:     if (lower_upper.second == s.end())
 40:         cout << "\nThe upper bound of 2 is at the end of the range.";
 41:     else
 42:         cout << "\nThe upper bound of 2 is " << *lower_upper.first << ".";

 44:     lower_upper = s.equal_range(5);
 45:     if (lower_upper.first == s.end())
 46:         cout << "\nThe lower bound of 5 is at the end of the range.";
 47:     else
 48:         cout << "\nThe lower bound of 5 is " << *lower_upper.first << ".";
 49:     if (lower_upper.second == s.end())
 50:         cout << "\nThe upper bound of 5 is at the end of the range.";
 51:     else
 52:         cout << "\nThe upper bound of 5 is " << *lower_upper.first << ".";

 54:     lower_upper = s.equal_range(6);
 55:     if (lower_upper.first == s.end())
 56:         cout << "\nThe lower bound of 6 is at the end of the range.";
 57:     else
 58:         cout << "\nThe lower bound of 6 is " << *lower_upper.first << ".";
 59:     if (lower_upper.second == s.end())
 60:         cout << "\nThe upper bound of 6 is at the end of the range.";
 61:     else
 62:         cout << "\nThe upper bound of 6 is " << *lower_upper.first << ".";

 64:     lower_upper = s.equal_range(10);
 65:     if (lower_upper.first == s.end())
 66:         cout << "\nThe lower bound of 10 is at the end of the range.";
 67:     else
 68:         cout << "\nThe lower bound of 10 is " << *lower_upper.first << ".";
 69:     if (lower_upper.second == s.end())
 70:         cout << "\nThe upper bound of 10 is at the end of the range.";
 71:     else
 72:         cout << "\nThe upper bound of 10 is " << *lower_upper.first << ".";

 74:     lower_upper = s.equal_range(12);
 75:     if (lower_upper.first == s.end())
 76:         cout << "\nThe lower bound of 12 is at the end of the range.";
 77:     else
 78:         cout << "\nThe lower bound of 12 is " << *lower_upper.first << ".";
 79:     if (lower_upper.second == s.end())
 80:         cout << "\nThe upper bound of 12 is at the end of the range.";
 81:     else
 82:         cout << "\nThe upper bound of 12 is " << *lower_upper.first << ".";

 84:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 85: }