Template Function thrust::equal_range(ForwardIterator, ForwardIterator, const T&, StrictWeakOrdering)¶
Function Documentation¶
-
template<class
ForwardIterator, classT, classStrictWeakOrdering>
thrust::pair<ForwardIterator, ForwardIterator>thrust::equal_range(ForwardIterator first, ForwardIterator last, const T &value, StrictWeakOrdering comp) equal_rangeis a version of binary search: it attempts to find the element value in an ordered range[first, last). The value returned byequal_rangeis essentially a combination of the values returned bylower_boundandupper_bound:it returns apairof iteratorsiandjsuch thatiis the first position where value could be inserted without violating the ordering andjis the last position where value could be inserted without violating the ordering. It follows that every element in the range[i, j)is equivalent to value, and that[i, j)is the largest subrange of[first, last)that has this property.This version of
equal_rangereturns apairof iterators[i, j).iis the furthermost iterator in[first, last)such that, for every iteratorkin[first, i),comp(*k, value)istrue.jis the furthermost iterator in[first, last)such that, for every iteratorkin[first, last),comp(value, *k)isfalse. For every iteratorkin[i, j), neithercomp(value, *k)norcomp(*k, value)istrue.The following code snippet demonstrates how to use
equal_rangeto search for values in a ordered range.- Return
A
pairof iterators[i, j)that define the range of equivalent elements.- 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::equal_range(input.begin(), input.end(), 0, thrust::less<int>()); // returns [input.begin(), input.begin() + 1) thrust::equal_range(input.begin(), input.end(), 1, thrust::less<int>()); // returns [input.begin() + 1, input.begin() + 1) thrust::equal_range(input.begin(), input.end(), 2, thrust::less<int>()); // returns [input.begin() + 1, input.begin() + 2) thrust::equal_range(input.begin(), input.end(), 3, thrust::less<int>()); // returns [input.begin() + 2, input.begin() + 2) thrust::equal_range(input.begin(), input.end(), 8, thrust::less<int>()); // returns [input.begin() + 4, input.end) thrust::equal_range(input.begin(), input.end(), 9, thrust::less<int>()); // returns [input.end(), input.end)
- See
- See
lower_bound- See
upper_bound- See
binary_search