C++ std::is_sorted_until() Function

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.
auto it = std::is_sorted_until(v.begin(), v.end());
std::is_sorted_until(v.begin(), v.end(), std::greater<int>());
| Call | What 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
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
| Parameter | Description |
|---|
| first, last | Required. The range to check. |
| comp | Optional. A comparison function that defines the order. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points at the first element that is out of order. |
last | Returned when the entire range is sorted. |
C++ Examples
Example 1 — Length of the sorted part
Find where the order breaks.
#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, 2 and 5 are in order.
- The next value, 3, is smaller than 5, so it breaks the order.
- 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.
#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
- Every neighbouring pair is in order.
- The function reaches the end without finding a problem.
- It returns
v.end(), which we test with it == v.end().
Example 3 — Descending run
Check a descending order.
#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
- With
std::greater, a good order means each value is not larger than the one before. - 9 then 7 is fine, but 8 is larger than 7.
- 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.
About the author
Developer, cloud engineer, and technical writer
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