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






Illustrating the generic binary search algorithms

 
 

#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
 */        
  








Related examples in the same category

1.Use binary_search to locate a value in a vector
2.binary_search a list