◆ is_sorted() [2/4]

template<typename ForwardIterator >
bool thrust::is_sorted ( ForwardIterator  first,
ForwardIterator  last 

is_sorted returns true if the range [first, last) is sorted in ascending order, and false otherwise.

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

firstThe beginning of the sequence.
lastThe end of the sequence.
true, if the sequence is sorted; false, otherwise.
Template Parameters
ForwardIteratoris a model of Forward Iterator, ForwardIterator's value_type is a model of LessThan Comparable, and the ordering on objects of ForwardIterator's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements.

The following code demonstrates how to use is_sorted to test whether the contents of a device_vector are stored in ascending order.

#include <thrust/sort.h>
#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(v.begin(), v.end());
// result == false
thrust::sort(v.begin(), v.end());
result = thrust::is_sorted(v.begin(), v.end());
// result == true
See also