C++ std::is_sorted() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++11

Definition

In C++, std::is_sorted() tells you if a range is already in order. By default it checks for ascending order with <.

It is handy as a quick safety check before using algorithms that need sorted data, such as std::binary_search().

Syntax

Add #include <algorithm>, then call std::is_sorted(). Start with these two lines.

C++
std::is_sorted(v.begin(), v.end());
std::is_sorted(v.begin(), v.end(), std::greater<int>());
CallWhat it does
std::is_sorted(v.begin(), v.end());Check for ascending order.
std::is_sorted(v.begin(), v.end(), std::greater<int>());Check for descending order.
Library declarations
C++
template<class ForwardIt>
bool is_sorted(ForwardIt first, ForwardIt last);

template<class ForwardIt, class Compare>
bool is_sorted(ForwardIt first, ForwardIt last, Compare comp);

Parameters

ParameterDescription
first, lastRequired. The range to check.
compOptional. A comparison function that defines the order.

Return Value

Return ValueDescription
trueThe range is sorted (or has fewer than two elements).
falseSome element is out of order.

C++ Examples

Example 1 — Sorted data

Confirm that a vector is in order.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3, 5};

    bool ok = std::is_sorted(v.begin(), v.end());

    std::cout << (ok ? "Sorted" : "Not sorted") << '\n';
    return 0;
}

How It Works

  1. Each value is not smaller than the one before it.
  2. is_sorted() checks all neighbouring pairs.
  3. No pair is out of order, so the result is true.

Example 2 — Unsorted data

A single wrong pair makes the result false.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {3, 1, 2};

    bool ok = std::is_sorted(v.begin(), v.end());

    std::cout << std::boolalpha << "Sorted: " << ok << '\n';
    return 0;
}

How It Works

  1. The first pair is 3 and 1, which is already out of order.
  2. The function can stop right away.
  3. It returns false.

Example 3 — Check both directions

Test ascending and descending order.

C++
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {9, 7, 3};

    bool asc = std::is_sorted(v.begin(), v.end());
    bool desc = std::is_sorted(v.begin(), v.end(), std::greater<int>());

    std::cout << std::boolalpha;
    std::cout << "Ascending? " << asc << '\n';
    std::cout << "Descending? " << desc << '\n';
    return 0;
}

How It Works

  1. By default the check uses <, so 9, 7, 3 is not ascending.
  2. With std::greater<int>() the check means "each value is not less than the next".
  3. The vector is sorted in descending order.

Frequently Asked Questions

In C++, std::is_sorted() checks whether a range is sorted. Include <algorithm> to use it.
Include <algorithm>. It needs C++11 or newer, so compile with -std=c++11 or later.
It returns true if the range is sorted according to the comparison, and false otherwise.
Yes. std::is_sorted() returns true for an empty range or a single element.
Pass std::greater<int>() as the third argument.

Next: is_sorted_until()

Finds the first element that breaks the sorted order.

is_sorted_until() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful