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; });
Call
What 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
Parameter
Description
first, last
Required. A partitioned range.
p
Required. The predicate that was used to partition the range.
Return Value
Return Value
Description
Iterator
Points at the first element for which the predicate is false.
last
Returned 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;
}
Output
First odd at index 3 (value 1)
How It Works
The vector is already partitioned: all even numbers are before all odd numbers.
partition_point() uses a binary search to find the change.
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;
}
Output
All values match
How It Works
Every number passes the even test.
There is no border inside the range.
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;
}
Output
First value not below 35: 40
How It Works
In sorted data, every number below 35 comes before every number at or above 35.
That means the vector is partitioned for this test.
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.