On this page
std::partition_point
| Defined in header <algorithm> | ||
|---|---|---|
|  | (since C++11) (until C++20) | |
|  | (since C++20) | 
Examines the partitioned (as if by std::partition) range [first, last) and locates the end of the first partition, that is, the first element that does not satisfy p or last if all elements satisfy p.
Parameters
| first, last | - | the partitioned range of elements to examine | 
| p | - | unary predicate which returns  truefor the elements found in the beginning of the range.The expression  | 
| Type requirements | ||
| - ForwardItmust meet the requirements of LegacyForwardIterator. | ||
| - UnaryPredicatemust meet the requirements of Predicate. | ||
Return value
The iterator past the end of the first partition within [first, last) or last if all elements satisfy p.
Complexity
Given N = std::distance(first, last), performs O(log N) applications of the predicate p.
However, for non-LegacyRandomAccessIterators, the number of iterator increments is O(N).
Notes
This algorithm is a more general form of std::lower_bound, which can be expressed in terms of std::partition_point with the predicate [&](auto const& e) { return e < value; });.
Possible implementation
|  | 
Example
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
 
auto print_seq = [](auto rem, auto first, auto last)
{
    for (std::cout << rem; first != last; std::cout << *first++ << ' ') {}
    std::cout << '\n';
};
 
int main()
{
    std::array v {1, 2, 3, 4, 5, 6, 7, 8, 9};
 
    auto is_even = [](int i) { return i % 2 == 0; };
 
    std::partition(v.begin(), v.end(), is_even);
    print_seq("After partitioning, v: ", v.cbegin(), v.cend());
 
    const auto pp = std::partition_point(v.cbegin(), v.cend(), is_even);
    const auto i = std::distance(v.cbegin(), pp);
    std::cout << "Partition point is at " << i << "; v[" << i << "] = " << *pp << '\n';
 
    print_seq("First partition (all even elements): ", v.cbegin(), pp);
    print_seq("Second partition (all odd elements): ", pp, v.cend());
}Possible output:
After partitioning, v: 8 2 6 4 5 3 7 1 9
Partition point is at 4; v[4] = 5
First partition (all even elements): 8 2 6 4
Second partition (all odd elements): 5 3 7 1 9See also
| (C++11)
        | finds the first element satisfying specific criteria (function template) | 
| (C++11)
        | checks whether a range is sorted into ascending order (function template) | 
| returns an iterator to the first element not less than the given value (function template) | |
| (C++20)
        | locates the partition point of a partitioned range (niebloid) | 
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
 https://en.cppreference.com/w/cpp/algorithm/partition_point