Source of binary_search1a.cpp


  1: //binary_search1a.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 "
 11:         "binary_search() algorithm\n(default version) to determine "
 12:         "whether a given integer is one of the\nvalues in a vector "
 13:         "of integers that are sorted in ascending order.";
 14:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 16:     int a[] = {1, 2, 3, 4, 5, 6, 7, 9, 10};
 17:     vector<int> v(a, a+9);
 18:     cout << "\nHere are the values in the vector:\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:     if (binary_search(v.begin(), v.end(), 3))
 24:         cout << "\nThe value 3 was found.";
 25:     else
 26:         cout << "\nThe value 3 was not found.";
 27:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 29:     if (binary_search(v.begin(), v.end(), 8))
 30:         cout << "\nThe value 8 was found.";
 31:     else
 32:         cout << "\nThe value 8 was not found.";
 33:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 34: }