Template Function thrust::binary_search(ForwardIterator, ForwardIterator, const T&, StrictWeakOrdering)

Function Documentation

template<class ForwardIterator, class T, class StrictWeakOrdering>
bool thrust::binary_search(ForwardIterator first, ForwardIterator last, const T &value, StrictWeakOrdering comp)

binary_search is a version of binary search: it attempts to find the element value in an ordered range [first, last). It returns true if an element that is equivalent to value is present in [first, last) and false if no such element exists. Specifically, this version returns true if and only if there exists an iterator i in [first, last) such that comp(*i, value) and comp(value, *i) are both false.

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), otherwise false.

Parameters
  • first: The beginning of the ordered sequence.

  • last: The end of the ordered sequence.

  • value: The value to be searched.

  • comp: The comparison operator.

Template Parameters

#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.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, thrust::less<int>()); // returns true
thrust::binary_search(input.begin(), input.end(), 1, thrust::less<int>()); // returns false
thrust::binary_search(input.begin(), input.end(), 2, thrust::less<int>()); // returns true
thrust::binary_search(input.begin(), input.end(), 3, thrust::less<int>()); // returns false
thrust::binary_search(input.begin(), input.end(), 8, thrust::less<int>()); // returns true
thrust::binary_search(input.begin(), input.end(), 9, thrust::less<int>()); // returns false

See

http://www.sgi.com/tech/stl/binary_search.html

See

lower_bound

See

upper_bound

See

equal_range