Template Function thrust::binary_search(ForwardIterator, ForwardIterator, const LessThanComparable&)¶
Function Documentation¶
-
template<class
ForwardIterator
, classLessThanComparable
>
boolthrust
::
binary_search
(ForwardIterator first, ForwardIterator last, const LessThanComparable &value) binary_search
is a version of binary search: it attempts to find the element value in an ordered range[first, last)
. It returnstrue
if an element that is equivalent tovalue
is present in[first, last)
andfalse
if no such element exists. Specifically, this version returnstrue
if and only if there exists an iteratori
in[first, last)
such that*i < value
andvalue < *i
are bothfalse
.The following code snippet demonstrates how to use
binary_search
to search for values in a ordered range.- Return
true
if an equivalent element exists in[first, last)
, otherwisefalse
.- Parameters
first
: The beginning of the ordered sequence.last
: The end of the ordered sequence.value
: The value to be searched.
- Template Parameters
ForwardIterator
: is a model of Forward Iterator.LessThanComparable
: is a model of LessThanComparable.
#include <thrust/binary_search.h> #include <thrust/device_vector.h> ... thrust::device_vector<int> input(5); input[0] = 0; input[1] = 2; input[2] = 5; input[3] = 7; input[4] = 8; thrust::binary_search(input.begin(), input.end(), 0); // returns true thrust::binary_search(input.begin(), input.end(), 1); // returns false thrust::binary_search(input.begin(), input.end(), 2); // returns true thrust::binary_search(input.begin(), input.end(), 3); // returns false thrust::binary_search(input.begin(), input.end(), 8); // returns true thrust::binary_search(input.begin(), input.end(), 9); // returns false
- See
- See
lower_bound
- See
upper_bound
- See
equal_range