◆ is_sorted() [3/4]

template<typename DerivedPolicy , typename ForwardIterator , typename Compare >
__host__ __device__ bool thrust::is_sorted ( const thrust::detail::execution_policy_base< DerivedPolicy > &  exec,
ForwardIterator  first,
ForwardIterator  last,
Compare  comp 

is_sorted returns true if the range [first, last) is sorted in ascending order accoring to a user-defined comparison operation, and false otherwise.

Specifically, this version of is_sorted returns false if for some iterator i in the range [first, last - 1) the expression comp(*(i + 1), *i) is true.

The algorithm's execution is parallelized as determined by exec.

execThe execution policy to use for parallelization.
firstThe beginning of the sequence.
lastThe end of the sequence.
compComparison operator.
true, if the sequence is sorted according to comp; false, otherwise.
Template Parameters
DerivedPolicyThe name of the derived execution policy.
ForwardIteratoris a model of Forward Iterator, and ForwardIterator's value_type is convertible to both StrictWeakOrdering's first_argument_type and second_argument_type.
Compareis a model of Strict Weak Ordering.

The following code snippet demonstrates how to use is_sorted to test whether the contents of a device_vector are stored in descending order using the thrust::device execution policy for parallelization:

#include <thrust/sort.h>
thrust::device_vector<int> v(6);
v[0] = 1;
v[1] = 4;
v[2] = 2;
v[3] = 8;
v[4] = 5;
v[5] = 7;
bool result = thrust::is_sorted(thrust::device, v.begin(), v.end(), comp);
// result == false
thrust::sort(v.begin(), v.end(), comp);
result = thrust::is_sorted(thrust::device, v.begin(), v.end(), comp);
// result == true
See also