Adobe Systems Incorporated


Category: algorithms Component type: function


Is_sorted is an overloaded name; there are actually two is_sorted functions.

template <class ForwardIterator>
bool is_sorted(ForwardIterator first, ForwardIterator last)

template <class ForwardIterator, class StrictWeakOrdering>
bool is_sorted(ForwardIterator first, ForwardIterator last,
               StrictWeakOrdering comp)


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

The two versions of is_sorted differ in how they define whether one element is less than another. The first version compares objects using operator<, and the second compares objects using the functors comp. The first version of is_sorted returns true if and only if, for every iterator i in the range [first, last - 1), *(i + 1) < *i is false. The second version returns true if and only if, for every iterator i in the range [first, last - 1), comp(*(i + 1), i) is false


Defined in algo.h.

Requirements on types

For the first version:

For the second version:

  • ForwardIterator is a model of ForwardIterator.
  • StrictWeakOrdering is a model of StrictWeakOrdering.
  • ForwardIterator's value type is convertible to StrictWeakOrdering's argument type.


  • [first, last) is a valid range.


Linear. Zero comparisons if [first, last) is an empty range, otherwise at most (last - first) - 1 comparisons.


int A[] = {1, 4, 2, 8, 5, 7};
const int N = sizeof(A) / sizeof(int);

assert(!is_sorted(A, A + N));
sort(A, A + N);
assert(is_sorted(A, A + N));


See also

sort, stable_sort, partial_sort, partial_sort_copy, sort_heap, binary_search, lower_bound, upper_bound, less<T>, StrictWeakOrdering, LessThanComparable

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google