C++ std::is_sorted_until() Function

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

Definition

In C++, std::is_sorted_until() returns an iterator to the first element that is out of order. Everything before that point is sorted.

If the whole range is sorted, it returns the end iterator. It tells you where the sorted part stops.

Syntax

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

C++
auto it = std::is_sorted_until(v.begin(), v.end());
std::is_sorted_until(v.begin(), v.end(), std::greater<int>());
CallWhat it does
auto it = std::is_sorted_until(v.begin(), v.end());Find the end of the sorted prefix.
std::is_sorted_until(v.begin(), v.end(), std::greater<int>());Use a descending order.
Library declarations
C++
template<class ForwardIt>
ForwardIt is_sorted_until(ForwardIt first, ForwardIt last);

template<class ForwardIt, class Compare>
ForwardIt is_sorted_until(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
IteratorPoints at the first element that is out of order.
lastReturned when the entire range is sorted.

C++ Examples

Example 1 — Length of the sorted part

Find where the order breaks.

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

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

    auto it = std::is_sorted_until(v.begin(), v.end());

    std::cout << "Sorted part length: " << (it - v.begin()) << '\n';
    return 0;
}

How It Works

  1. 1, 2 and 5 are in order.
  2. The next value, 3, is smaller than 5, so it breaks the order.
  3. The iterator points at index 3, which is also the length of the sorted part.

Example 2 — Fully sorted

A sorted range returns the end iterator.

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

int main() {
    std::vector<int> v = {2, 4, 6, 8};

    auto it = std::is_sorted_until(v.begin(), v.end());

    if (it == v.end()) {
        std::cout << "Whole vector is sorted\n";
    }
    return 0;
}

How It Works

  1. Every neighbouring pair is in order.
  2. The function reaches the end without finding a problem.
  3. It returns v.end(), which we test with it == v.end().

Example 3 — Descending run

Check a descending order.

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

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

    auto it = std::is_sorted_until(v.begin(), v.end(), std::greater<int>());

    std::cout << "Descending run length: " << (it - v.begin()) << '\n';
    return 0;
}

How It Works

  1. With std::greater, a good order means each value is not larger than the one before.
  2. 9 then 7 is fine, but 8 is larger than 7.
  3. The order breaks at index 2, so the run has length 2.

Frequently Asked Questions

In C++, std::is_sorted_until() finds the first element that breaks the sorted order. Include <algorithm> to use it.
Include <algorithm>. It needs C++11 or newer, so compile with -std=c++11 or later.
It returns an iterator to the first element that is out of order, or the last iterator if the whole range is sorted.
is_sorted() returns true or false. is_sorted_until() tells you where the sorted part ends.
It returns the end iterator, which equals the begin iterator.

Next: iter_swap()

Swaps the values that two iterators point to.

iter_swap() 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.

20 people found this page helpful