std::ranges::partition_point
| 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 > |
(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 > |
(2) | (since C++20) |
Examines the partitioned (as if by ranges::partition) range [first, last) or r and locates the end of the first partition, that is, 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, that is:
- Explicit template argument lists may not be specified when calling any of them.
- None of them is visible to argument-dependent lookup.
- When one of them is found by normal unqualified lookup for the name to the left of the function-call operator, it inhibits argument-dependent lookup.
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 examine |
| r | - | the partially-ordered range to examine |
| pred | - | predicate to apply to the projected elements |
| proj | - | 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), performs O(log N) applications of the predicate pred and projection proj.
However, if sentinels don't model std::sized_sentinel_for<I>, the number of iterator increments is O(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
#include <algorithm> #include <array> #include <iostream> #include <iterator> int main() { std::array v = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; namespace ranges = std::ranges; auto is_even = [](int i){ return i % 2 == 0; }; ranges::partition(v, is_even); auto p = ranges::partition_point(v.begin(), v.end(), is_even); std::cout << "Before partition:\n "; ranges::copy(v.begin(), p, std::ostream_iterator<int>(std::cout, " ")); std::cout << "\nAfter partition:\n "; ranges::copy(p, v.end(), std::ostream_iterator<int>(std::cout, " ")); }
Output:
Before partition:
8 2 6 4
After partition:
5 3 7 1 9See also
| (C++20) |
checks whether a range is sorted into ascending order (niebloid) |
| (C++20) |
returns an iterator to the first element not less than the given value (niebloid) |
| (C++11) |
locates the partition point of a partitioned range (function template) |