C++ std::is_partitioned() Function

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

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.

C++
std::is_partitioned(v.begin(), v.end(), pred);
std::is_partitioned(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
CallWhat 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
C++
template<class InputIt, class UnaryPred>
bool is_partitioned(InputIt first, InputIt last, UnaryPred p);

Parameters

ParameterDescription
first, lastRequired. The range to check.
pRequired. The predicate that defines the groups.

Return Value

Return ValueDescription
trueAll true elements come before all false elements (an empty range also counts).
falseA true element appears after a false element.

C++ Examples

Example 1 — A partitioned range

All evens come before all odds.

C++
#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

  1. The even numbers 2, 4 and 6 are first.
  2. After them only odd numbers follow.
  3. 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.

C++
#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

  1. 2 is even, then 1 is odd, so the true group has ended.
  2. But 4 is even again, which is another true element after a false one.
  3. The result is false.

Example 3 — Before and after partition

Call partition and check again.

C++
#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;
}

How It Works

  1. The lambda is stored in isEven so both calls use the same rule.
  2. At first an odd 5 comes before the even 2, so the check fails.
  3. 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.

Next: is_permutation()

Checks whether one range is a rearrangement of another.

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

11 people found this page helpful