C++ std::is_partitioned() Function

Definition
In C++, std::is_partitioned() returns true if all elements for which the predicate is true come before all elements for which it is false.
It does not move anything. It is a quick check before using std::partition_point().
Syntax
Add #include <algorithm>, then call std::is_partitioned(). Start with these two lines.
std::is_partitioned(v.begin(), v.end(), pred);
std::is_partitioned(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
| Call | What it does |
|---|
std::is_partitioned(v.begin(), v.end(), pred); | Is the range split by pred? |
std::is_partitioned(v.begin(), v.end(), [](int n) { return n % 2 == 0; }); | Check an even-first split. |
Library declarations
template<class InputIt, class UnaryPred>
bool is_partitioned(InputIt first, InputIt last, UnaryPred p);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The range to check. |
| p | Required. The predicate that defines the groups. |
Return Value
| Return Value | Description |
|---|
true | All true elements come before all false elements (an empty range also counts). |
false | A true element appears after a false element. |
C++ Examples
Example 1 — A partitioned range
All evens come before all odds.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {2, 4, 6, 1, 3, 5};
bool ok = std::is_partitioned(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
std::cout << std::boolalpha << "Partitioned: " << ok << '\n';
return 0;
}
How It Works
- The even numbers 2, 4 and 6 are first.
- After them only odd numbers follow.
- The two groups do not mix, so the answer is
true.
Example 2 — A mixed range
An even number after an odd one breaks the split.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {2, 1, 4};
bool ok = std::is_partitioned(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
std::cout << std::boolalpha << "Partitioned: " << ok << '\n';
return 0;
}
How It Works
- 2 is even, then 1 is odd, so the true group has ended.
- But 4 is even again, which is another true element after a false one.
- The result is
false.
Example 3 — Before and after partition
Call partition and check again.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 2, 8, 3};
auto isEven = [](int n) { return n % 2 == 0; };
std::cout << std::boolalpha;
std::cout << "Before: " << std::is_partitioned(v.begin(), v.end(), isEven) << '\n';
std::partition(v.begin(), v.end(), isEven);
std::cout << "After: " << std::is_partitioned(v.begin(), v.end(), isEven) << '\n';
return 0;
}
Before: false
After: true
How It Works
- The lambda is stored in
isEven so both calls use the same rule. - At first an odd 5 comes before the even 2, so the check fails.
partition() moves the even numbers to the front, and the second check passes.
Frequently Asked Questions
In C++, std::is_partitioned() checks whether a range is partitioned by a condition. 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 every element that satisfies the predicate comes before every element that does not.
Yes. is_partitioned() returns true for an empty range.
is_partitioned() only checks two groups defined by a predicate. is_sorted() checks the full order of all elements.
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.
11 people found this page helpful