C++ std::is_sorted() Function

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.
std::is_sorted(v.begin(), v.end());
std::is_sorted(v.begin(), v.end(), std::greater<int>());
| Call | What 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
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
| Parameter | Description |
|---|
| first, last | Required. The range to check. |
| comp | Optional. A comparison function that defines the order. |
Return Value
| Return Value | Description |
|---|
true | The range is sorted (or has fewer than two elements). |
false | Some element is out of order. |
C++ Examples
Example 1 — Sorted data
Confirm that a vector is in order.
#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
- Each value is not smaller than the one before it.
is_sorted() checks all neighbouring pairs.- No pair is out of order, so the result is
true.
Example 2 — Unsorted data
A single wrong pair makes the result false.
#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
- The first pair is 3 and 1, which is already out of order.
- The function can stop right away.
- It returns
false.
Example 3 — Check both directions
Test ascending and descending order.
#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;
}
Ascending? false
Descending? true
How It Works
- By default the check uses
<, so 9, 7, 3 is not ascending. - With
std::greater<int>() the check means "each value is not less than the next". - 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.
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.
10 people found this page helpful