VOOZH about

URL: https://www.geeksforgeeks.org/cpp/stdis_sorted-in-cpp/

⇱ is_sorted() in C++ STL - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

is_sorted() in C++ STL

Last Updated : 3 Mar, 2026

In C++, is_sorted() is a built-in function used to check whether the elements in the given range are sorted or not in ascending order by default. You can also provide a custom comparator to check for descending order or any other sorting criteria.

For example: Let's create a vector and check if the elements are sorted using is_sorted() method.


Output
Sorted

Syntax

is_sorted() is defined inside <algorithm> header file.

is_sorted(first, last, comp);

Parameters:

  • first: Iterator to the first element of given range.
  • last: Iterator to the element just after the last element of given range.
  • comp(optional): It is a custom comparison function used to change the sorting order to be checked. By default, it uses the < operator which check if the range is sorted in ascending order.

Return Value: Returns true, if the range is sorted otherwise it returns false.

Examples of is_sorted()

The following examples demonstrates the use of is_sorted() method in different scenario:

Check if an Array is Sorted in Ascending Order


Output
Given Array is Sorted

Check if Part of a Vector is Sorted

We can check if a given vector or array is sorted within a specified range.


Output
Sorted

Explanation: is_sorted(v.begin()+2, v.end()) check if the vector is sorted from index '2' till the end.

Check if a List is Sorted in Descending Order


Output
Given Vector is Sorted

Check if Vector of String is in Lexicographically Ordered


Output
Given Vector is Sorted

Check if Set is Sorted in Descending Order


Output
Not Sorted

Explanation: The set is already sorted in ascending order. We have used is_sorted() with custom comparator to check if the set is in descending order.

Comment
Article Tags: