Source of equal_range1a.cpp


  1: //equal_range1a.cpp

  3: #include <iostream>
  4: #include <vector>
  5: #include <algorithm>
  6: using namespace std;

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the use of the STL equal_range() "
 11:         "algorithm (default\nversion) to find the lower bound and upper "
 12:         "bound locations of a given target\nvalue in a vector of integers "
 13:         "sorted in ascending order.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 16:     int a[] = {2, 3, 5, 6, 7, 7, 7,  8, 9, 10};
 17:     vector<int> v(a, a+10);
 18:     cout << "\nHere are the contents of v:\n";
 19:     for (vector<int>::size_type i=0; i<v.size(); i++)
 20:         cout << v.at(i) << " ";
 21:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 23:     pair<vector<int>::iterator, vector<int>::iterator> bounds;

 25:     bounds = equal_range(v.begin(), v.end(), 3);
 26:     if (bounds.first != v.end())
 27:         cout << "\nLower bound of 3 in v = " << *bounds.first;
 28:     if (bounds.first != v.end())
 29:         cout << "\nUpper bound of 3 in v = " << *bounds.second;
 30:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 32:     bounds = equal_range(v.begin(), v.end(), 4);
 33:     if (bounds.first != v.end())
 34:         cout << "\nLower bound of 4 in v = " << *bounds.first;
 35:     if (bounds.first != v.end())
 36:         cout << "\nUpper bound of 4 in v = " << *bounds.second;
 37:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 39:     bounds = equal_range(v.begin(), v.end(), 5);
 40:     if (bounds.first != v.end())
 41:         cout << "\nLower bound of 5 in v = " << *bounds.first;
 42:     if (bounds.first != v.end())
 43:         cout << "\nUpper bound of 5 in v = " << *bounds.second;
 44:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 46:     bounds = equal_range(v.begin(), v.end(), 7);
 47:     if (bounds.first != v.end())
 48:         cout << "\nLower bound of 7 in v = " << *bounds.first;
 49:     cout << "\nThis is the first of the three 7's, since the value "
 50:         "before this 7 is " << *(bounds.first-1) << ".";
 51:     if (bounds.first != v.end())
 52:         cout << "\nUpper bound of 7 in v = " << *bounds.second;
 53:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 55:     bounds = equal_range(v.begin(), v.end(), 0);
 56:     if (bounds.first != v.end())
 57:         cout << "\nLower bound of 0 in v = " << *bounds.first;
 58:     if (bounds.first != v.end())
 59:         cout << "\nUpper bound of 0 in v = " << *bounds.second;
 60:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 62:     bounds = equal_range(v.begin(), v.end(), 15);
 63:     if (bounds.first != v.end())
 64:         cout << "\nLower bound of 15 in v = " << *bounds.first;
 65:     if (bounds.first != v.end())
 66:         cout << "\nUpper bound of 15 in v = " << *bounds.second;
 67:     cout << "\nNote that both the lower and upper bound locations "
 68:         "\nof 15 are the end (one-past-the-last) vector position.";
 69:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 70: }