Source of upper_bound1a.cpp


  1: //upper_bound1a.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 upper_bound() "
 11:         "algorithm (default\nversion) to find the upper bound location "
 12:         "of a given target value in a vector\nof integers sorted in "
 13:         "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:     vector<int>::iterator upper;

 25:     upper = upper_bound(v.begin(), v.end(), 3);
 26:     if (upper != v.end())
 27:         cout << "\nUpper bound of 3 in v = " << *upper;
 28:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 30:     upper = upper_bound(v.begin(), v.end(), 4);
 31:     if (upper != v.end())
 32:         cout << "\nUpper bound of 4 in v = " << *upper;
 33:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 35:     upper = upper_bound(v.begin(), v.end(), 5);
 36:     if (upper != v.end())
 37:         cout << "\nUpper bound of 5 in v = " << *upper;
 38:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 40:     upper = upper_bound(v.begin(), v.end(), 7);
 41:     if (upper != v.end())
 42:         cout << "\nUpper bound of 7 in v = " << *upper;
 43:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 45:     upper = upper_bound(v.begin(), v.end(), 0);
 46:     if (upper != v.end())
 47:         cout << "\nUpper bound of 0 in v = " << *upper;
 48:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 50:     upper = upper_bound(v.begin(), v.end(), 15);
 51:     if (upper != v.end())
 52:         cout << "\nUpper bound of 15 in v = " << *upper;
 53:     cout << "\nNote that the upper bound location of 15 is "
 54:         "\nthe end (one-past-the-last) vector position.";
 55:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 56: }