thrust::binary_search
Defined in thrust/binary_search.h
- 
template<typename DerivedPolicy, typename ForwardIterator, typename T, typename StrictWeakOrdering>
 bool thrust::binary_search(const thrust::detail::execution_policy_base<DerivedPolicy> &exec, ForwardIterator first, ForwardIterator last, const T &value, StrictWeakOrdering comp)
- binary_searchis a version of binary search: it attempts to find the element value in an ordered range- [first, last). It returns- trueif an element that is equivalent to- valueis present in- [first, last)and- falseif no such element exists. Specifically, this version returns- trueif and only if there exists an iterator- iin- [first, last)such that- comp(*i, value)and- comp(value, *i)are both- false.- The algorithm’s execution is parallelized as determined by - exec.- The following code snippet demonstrates how to use - binary_searchto search for values in a ordered range using the- thrust::deviceexecution policy for parallelization:- #include <thrust/binary_search.h> #include <thrust/device_vector.h> #include <thrust/functional.h> #include <thrust/execution_policy.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(thrust::device, input.begin(), input.end(), 0, thrust::less<int>()); // returns true thrust::binary_search(thrust::device, input.begin(), input.end(), 1, thrust::less<int>()); // returns false thrust::binary_search(thrust::device, input.begin(), input.end(), 2, thrust::less<int>()); // returns true thrust::binary_search(thrust::device, input.begin(), input.end(), 3, thrust::less<int>()); // returns false thrust::binary_search(thrust::device, input.begin(), input.end(), 8, thrust::less<int>()); // returns true thrust::binary_search(thrust::device, input.begin(), input.end(), 9, thrust::less<int>()); // returns false - See also - lower_bound- See also - upper_bound- See also - equal_range- Parameters
- exec – The execution policy to use for parallelization. 
- 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
- DerivedPolicy – The name of the derived execution policy. 
- ForwardIterator – is a model of Forward Iterator. 
- T – is comparable to - ForwardIterator's- value_type.
- StrictWeakOrdering – is a model of Strict Weak Ordering. 
 
- Returns
- trueif an equivalent element exists in- [first, last), otherwise- false.