Template Function thrust::upper_bound(ForwardIterator, ForwardIterator, const T&, StrictWeakOrdering)¶
Function Documentation¶
-
template<class
ForwardIterator, classT, classStrictWeakOrdering>
ForwardIteratorthrust::upper_bound(ForwardIterator first, ForwardIterator last, const T &value, StrictWeakOrdering comp) upper_boundis a version of binary search: it attempts to find the element value in an ordered range[first, last). Specifically, it returns the last position where value could be inserted without violating the ordering. This version ofupper_bounduses function objectcompfor comparison and returns the furthermost iteratoriin[first, last)such that, for every iteratorjin[first, i),comp(value, *j)isfalse.The following code snippet demonstrates how to use
upper_boundto search for values in a ordered range.- Return
The furthermost iterator
i, such thatcomp(value, *i)isfalse.- 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
ForwardIterator: is a model of Forward Iterator.T: is comparable toForwardIterator'svalue_type.StrictWeakOrdering: is a model of Strict Weak Ordering.
#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::upper_bound(input.begin(), input.end(), 0, thrust::less<int>()); // returns input.begin() + 1 thrust::upper_bound(input.begin(), input.end(), 1, thrust::less<int>()); // returns input.begin() + 1 thrust::upper_bound(input.begin(), input.end(), 2, thrust::less<int>()); // returns input.begin() + 2 thrust::upper_bound(input.begin(), input.end(), 3, thrust::less<int>()); // returns input.begin() + 2 thrust::upper_bound(input.begin(), input.end(), 8, thrust::less<int>()); // returns input.end() thrust::upper_bound(input.begin(), input.end(), 9, thrust::less<int>()); // returns input.end()
- See
- See
lower_bound- See
equal_range- See
binary_search