std::partition_point(3)
You're on the std::partition_point(3) page from the C++ Standard Libary manual β yes, "Libary" is the header's spelling, I'm just the messenger π
.
πΈ NAME πΈ
std::partition_point - std::partition_point. That's the whole name entry, bestie; minimal, iconic, and we love that for it β¨.
β¨ Synopsis β¨
Defined in header <algorithm>
template< class ForwardIt, class UnaryPred > (since C++11)
ForwardIt partition_point( ForwardIt first, ForwardIt last,
(constexpr since C++20)
UnaryPred p );
It examines the partitioned 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.
π¨ If the elementselemof[first, last)are not partitioned with respect to the expressionbool(p(elem)), the behavior is undefined. No cap, that's the scary part.
π Parameters π
first,lastβ the partitioned range of elements to examine.pβ a unary predicate which returnstruefor the elements found in the beginning of the range.
The expression p(v) must be convertible to bool for every argument v of type (possibly const) VT, where VT is the value type of ForwardIt, regardless of value category, and must not modify v. Thus, a parameter type of VT& is not allowed, nor is VT unless for VT a move is equivalent to a copy (since C++11).
π¦ Type requirements π¦
ForwardItmust meet the requirements ofLegacyForwardIterator.UnaryPredmust meet the requirements ofPredicate.
π Return value π
The iterator past the end of the first partition within [first, last), or last if all elements satisfy p.
π₯ Complexity π₯
Given N as std::distance(first, last), performs O(log(N)) applications of the predicate p.
π 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 [&](const auto& e) { return e < value; });.
π Possible implementation π
template<class ForwardIt, class UnaryPred>
constexpr //< since C++20
ForwardIt partition_point(ForwardIt first, ForwardIt last, UnaryPred p)
{
for (auto length = std::distance(first, last); 0 < length; )
{
auto half = length / 2;
auto middle = std::next(first, half);
if (p(*middle))
{
first = std::next(middle);
length -= (half + 1);
}
else
length = half;
}
return first;
}
π Example π
// Run this code
#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 9
π§ See also π§
find,find_ifβ finds the first element satisfying specific criteriafind_if_notβ (function template) (C++11)is_sortedβ checks whether a range is sorted into ascending order (C++11) (function template)lower_boundβ returns an iterator to the first element not less than the given value (function template)ranges::partition_pointβ locates the partition point of a partitioned range (C++20) (niebloid)
Sign-off time: source http://cppreference.com, version 2024.06.10, dated 2024.06.10 β footer handled, periodt π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms