Source of lower_bound1a.cpp


  1: //lower_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 lower_bound() "
 11:         "algorithm (default\nversion) to find the lower 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 lower;

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

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

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

 40:     lower = lower_bound(v.begin(), v.end(), 7);
 41:     if (lower != v.end())
 42:         cout << "\nLower bound of 7 in v = " << *lower;
 43:     cout << "\nThis is the first of the three 7's, since the value "
 44:         "before this 7 is " << *(lower-1) << ".";
 45:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 47:     lower = lower_bound(v.begin(), v.end(), 0);
 48:     if (lower != v.end())
 49:         cout << "\nLower bound of 0 in v = " << *lower;
 50:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

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