cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Algorithms : binary_search
 
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Algorithms
algorithm:
· adjacent_find
· binary_search
· copy
· copy_backward
· count
· count_if
· equal
· equal_range
· fill
· fill_n
· find
· find_end
· find_first_of
· find_if
· for_each
· generate
· generate_n
· includes
· inplace_merge
· iter_swap
· lexicographical_compare
· lower_bound
· make_heap
· max
· max_element
· merge
· min
· min_element
· mismatch
· next_permutation
· nth_element
· partial_sort
· partial_sort_copy
· partition
· pop_heap
· prev_permutation
· push_heap
· random_shuffle
· remove
· remove_copy
· remove_copy_if
· remove_if
· replace
· replace_copy
· replace_copy_if
· replace_if
· reverse
· reverse_copy
· rotate
· rotate_copy
· search
· search_n
· set_difference
· set_intersection
· set_symmetric_difference
· set_union
· sort
· sort_heap
· stable_partition
· stable_sort
· swap
· swap_ranges
· transform
· unique
· unique_copy
· upper_bound

-

binary_search function template
template <class ForwardIterator, class T>
  bool binary_search ( ForwardIterator first, ForwardIterator last,
                       const T& value );

template <class ForwardIterator, class T, class Compare>
  bool binary_search ( ForwardIterator first, ForwardIterator last,
                       const T& value, Compare comp );
<algorithm>

Test if value exists in sorted array

Returns true if an element in the range [first,last) is equivalent to value, and false otherwise.

The comparison is performed using either operator< for the first version, or comp for the second: A value, a, is considered equivalent to another, b, when (!(a<b) && !(b<a)) or (!comp(a,b) && !comp(b,a))

For the function to yield the expected result, the elements in the range shall already be ordered according to the same criterion (operator< or comp).

The behavior of this function template is equivalent to:

template <class ForwardIterator, class T>
  bool binary_search ( ForwardIterator first, ForwardIterator last, const T& value )
{
  first = lower_bound(first,last,value);
  return (first!=last && !(value<*first));
}


Parameters

first, last
Forward iterators to the initial and final positions of the sequence to be searched. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
value
Element value to search for.
comp
Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.

Return value

true if an element in value is found, and false otherwise.

Example

// binary_search example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool myfunction (int i,int j) { return (i<j); }

int main () {
  int myints[] = {1,2,3,4,5,4,3,2,1};
  vector<int> v(myints,myints+9);                         // 1 2 3 4 5 4 3 2 1

  // using default comparison:
  sort (v.begin(), v.end());

  cout << "looking for a 3... ";
  if (binary_search (v.begin(), v.end(), 3))
    cout << "found!\n"; else cout << "not found.\n";

  // using myfunction as comp:
  sort (v.begin(), v.end(), myfunction);

  cout << "looking for a 6... ";
  if (binary_search (v.begin(), v.end(), 6, myfunction))
    cout << "found!\n"; else cout << "not found.\n";

  return 0;
}

Output:

looking for a 3... found!
looking for a 6... not fount.

Complexity

At most, logarithmic number of comparisons and linear number of steps in the length of [first,last) +2.
If used with random-access iterators, number of steps is reduced to logarithmic.

See also

find Find value in range (function template)
lower_bound Return iterator to lower bound (function template)
upper_bound Return iterator to upper bound (function template)
equal_range Get subrange of equal elements (function template)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us