C++ std::partition_point() Function

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

Definition

In C++, std::partition_point() uses a binary search to find where a partitioned range changes from "predicate true" to "predicate false". It returns an iterator to the first element for which the predicate is false.

The range must already be partitioned, for example after std::partition(), or sorted data checked with a threshold.

Syntax

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

C++
auto it = std::partition_point(v.begin(), v.end(), pred);
std::partition_point(v.begin(), v.end(), [](int n) { return n < 35; });
CallWhat it does
auto it = std::partition_point(v.begin(), v.end(), pred);Find the first element where pred is false.
std::partition_point(v.begin(), v.end(), [](int n) { return n < 35; });Find a threshold in sorted data.
Library declarations
C++
template<class ForwardIt, class UnaryPred>
ForwardIt partition_point(ForwardIt first, ForwardIt last, UnaryPred p);

Parameters

ParameterDescription
first, lastRequired. A partitioned range.
pRequired. The predicate that was used to partition the range.

Return Value

Return ValueDescription
IteratorPoints at the first element for which the predicate is false.
lastReturned when the predicate is true for every element.

C++ Examples

Example 1 — Find the border

Locate the first odd number after an even-first split.

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

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

    auto it = std::partition_point(v.begin(), v.end(), [](int n) { return n % 2 == 0; });

    std::cout << "First odd at index " << (it - v.begin()) << " (value " << *it << ")\n";
    return 0;
}

How It Works

  1. The vector is already partitioned: all even numbers are before all odd numbers.
  2. partition_point() uses a binary search to find the change.
  3. The first element where the test fails is 1, at index 3.

Example 2 — Everything matches

No false element means the end iterator.

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

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

    auto it = std::partition_point(v.begin(), v.end(), [](int n) { return n % 2 == 0; });

    if (it == v.end()) {
        std::cout << "All values match\n";
    }
    return 0;
}

How It Works

  1. Every number passes the even test.
  2. There is no border inside the range.
  3. So the function returns v.end().

Example 3 — Threshold in sorted data

Sorted data is partitioned by any threshold test.

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

int main() {
    std::vector<int> v = {10, 20, 30, 40, 50};

    auto it = std::partition_point(v.begin(), v.end(), [](int n) { return n < 35; });

    std::cout << "First value not below 35: " << *it << '\n';
    return 0;
}

How It Works

  1. In sorted data, every number below 35 comes before every number at or above 35.
  2. That means the vector is partitioned for this test.
  3. The border sits at 40.

Frequently Asked Questions

In C++, std::partition_point() finds the border of a partitioned range. 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 for which the predicate is false, or the last iterator if the predicate is true for all of them.
A range that is already partitioned with the same predicate.
It uses a binary search, so it does not check every element.

Next: pop_heap()

Moves the top element of a heap to the end of the range.

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

10 people found this page helpful