std::ranges...ition_point(3)
πΈ NAME πΈ
std::ranges::partition_point β literally std::ranges::partition_point, bestie π
. We're on the std::ranges::partition_point(3) page from the C++ Standard Libary manual β¨ β manual name folded in near the top, no header-line copy-paste energy.
β¨ Synopsis β¨
Defined in header <algorithm>.
Call signature:
template< std::forward_iterator I, std::sentinel_for<I> S,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>>
Pred >
constexpr I
partition_point( I first, S last, Pred pred, Proj proj = {} );
(1) (since C++20)
template< ranges::forward_range R,
class Proj = std::identity,
std::indirect_unary_predicate<
std::projected<ranges::iterator_t<R>, Proj>> Pred >
constexpr ranges::borrowed_iterator_t<R>
partition_point( R&& r, Pred pred, Proj proj = {} );
(2) (since C++20)
okay so what does it actually do? It examines the partitioned (as if by ranges::partition) range [first, last) or r, and locates the end of the first partition. That's the projected element that does not satisfy pred, or last if all projected elements satisfy pred π«Ά.
The function-like entities described on this page are niebloids, bestie, which means:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
In practice, they may be implemented as function objects, or with special compiler extensions.
π Parameters π
first,lastβ iterator-sentinel defining the partially-ordered range to examinerβ the partially-ordered range to examinepredβ predicate to apply to the projected elementsprojβ projection to apply to the elements
π Return value π
The iterator past the end of the first partition within [first, last), or the iterator equal to last if all projected elements satisfy pred β¨.
π₯ Complexity π₯
Given N = ranges::distance(first, last), it performs O(log N) applications of the predicate pred and projection proj.
however bestie, if sentinels don't modelstd::sized_sentinel_for<I>, the number of iterator increments isO(N)π.
π¦ Notes π¦
This algorithm is a more general form of ranges::lower_bound, which can be expressed in terms of ranges::partition_point with the predicate [&](auto const& e) { return std::invoke(pred, e, value); });.
π 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::ranges::partition(v, is_even);
print_seq("After partitioning, v: ", v.cbegin(), v.cend());
const auto pp = std::ranges::partition_point(v, is_even);
const auto i = std::ranges::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: 2 4 6 8 5 3 7 1 9
Partition point is at 4; v[4] = 5
First partition (all even elements): 2 4 6 8
Second partition (all odd elements): 5 3 7 1 9
π See also π
ranges::is_sortedβ checks whether a range is sorted into ascending order;(C++20);(niebloid)ranges::lower_boundβ returns an iterator to the first element not less than the given value;(C++20);(niebloid)partition_pointβ locates the partition point of a partitioned range;(C++11);(function template)
okay that's the whole page, bestie β source/version: http://cppreference.com, date: 2024.06.10 ππ
β¨
π¦ 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