std::ranges::is_partitioned
From cppreference.com
| Defined in header <algorithm>
|
||
| Call signature |
||
template< std::input_iterator I, std::sentinel_for<I> S,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
constexpr bool is_partitioned( I first, S last, Pred pred, Proj proj = {} );
|
(1) | (since C++20) |
template< ranges::input_range R, class Proj = std::identity,
std::indirect_unary_predicate
<std::projected<ranges::iterator_t<R>, Proj>> Pred >
constexpr bool is_partitioned( R&& r, Pred pred, Proj proj = {} );
|
(2) | (since C++20) |
template< /*execution-policy*/ Ep,
std::random_access_iterator I, std::sized_sentinel_for<I> S,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
bool is_partitioned( Ep&& policy, I first, S last, Pred pred, Proj proj = {} );
|
(3) | (since C++26) |
template< /*execution-policy*/ Ep,
/*sized-random-access-range*/ R, class Proj = std::identity,
std::indirect_unary_predicate
<std::projected<ranges::iterator_t<R>, Proj>> Pred >
bool is_partitioned( Ep&& policy, R&& r, Pred pred, Proj proj = {} );
|
(4) | (since C++26) |
For the definition of /*execution-policy*/, see this page; for the definition of /*sized-random-access-range*/, see this page.
1,2) Checks whether the elements
e in the source range [first, last) or r are partitioned with respect to the expression bool(std::invoke(pred, std::invoke(proj, e))): all elements (projected by proj) satisfy pred appear before all elements that do not.3,4) Same as (1,2), but executed according to
policy.The function-like entities described on this page are algorithm function objects (informally known as niebloids), that is:
- 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.
Parameters
| first, last | - | the iterator-sentinel pair defining the source range |
| r | - | the source range |
| pred | - | the predicate to be applied to the (projected) elements |
| proj | - | the projection to be applied to the elements |
| policy | - | the execution policy to use |
Return value
true if the elements of the source range are partitioned as descibed above or the source range is empty, false otherwise.
Complexity
Given \(\scriptsize N\)N as ranges::distance(first, last) or ranges::distance(r):
1,2) At most \(\scriptsize N\)N applications of
pred and proj.3,4) \(\scriptsize \mathcal{O}(N)\)(N) applications of
pred and proj.Notes
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_parallel_algorithm |
202506L |
(C++26) | Parallel range algorithms, overloads (3,4) |
Possible implementation
struct is_partitioned_fn
{
template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const
{
for (; first != last; ++first)
if (!std::invoke(pred, std::invoke(proj, *first)))
break;
for (; first != last; ++first)
if (std::invoke(pred, std::invoke(proj, *first)))
return false;
return true;
}
template<ranges::input_range R, class Proj = std::identity,
std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>> Pred>
constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
}
template<ranges::forward_range R, class Proj = std::identity,
std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>> Pred>
constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::next(ranges::begin(r), ranges::end(r)),
std::ref(pred), std::ref(proj));
}
};
inline constexpr is_partitioned_fn is_partitioned;
|
Example
Run this code
#include <algorithm>
#include <array>
#include <numeric>
#include <print>
#include <utility>
int main()
{
std::array<int, 9> v;
auto print = [&v](bool o) { std::print("{} is partitioned: {}\n", v, o); };
auto is_even = [](int i) { return i % 2 == 0; };
std::iota(v.begin(), v.end(), 1); // or std::ranges::iota(v, 1);
print(std::ranges::is_partitioned(v, is_even));
std::ranges::partition(v, is_even);
print(std::ranges::is_partitioned(std::as_const(v), is_even));
std::ranges::reverse(v);
print(std::ranges::is_partitioned(v.cbegin(), v.cend(), is_even));
print(std::ranges::is_partitioned(v.crbegin(), v.crend(), is_even));
}
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9] is partitioned: false
[8, 2, 6, 4, 5, 3, 7, 1, 9] is partitioned: true
[9, 1, 7, 3, 5, 4, 6, 2, 8] is partitioned: false
[9, 1, 7, 3, 5, 4, 6, 2, 8] is partitioned: true
See also
(C++20) |
locates the partition point of a partitioned range (algorithm function object) |
(C++11) |
determines if the range is partitioned by the given predicate (function template) |
(C++20) |
divides a range of elements into two groups (algorithm function object) |