Illustrating the generic binary search algorithms : binary_search « STL Algorithms Binary search « C++ Tutorial






#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
  vector<int> v(5);
  bool found;

  for (int i = 0; i < 5; ++i)
     v[i] = i;

  found = binary_search(v.begin(), v.end(), 3);
  cout << found << " ";

  // Try searching for a value that's not present:
  found = binary_search (v.begin(), v.end(), 9);
  cout << found;

  return 0;
}
1 0








26.1.binary_search
26.1.1.Illustrating the generic binary search algorithms
26.1.2.Use binary_search to locate a value in a vector
26.1.3.binary_search a list
26.1.4.Use binary_search() to binary search a vector
26.1.5.Binary search after sorting