std::ranges..._partitioned(3)
π NAME π
Okay so bestie, welcome to the std::ranges..._partitioned(3) page from the C++ Standard Libary manual π The name up top is std::ranges::is_partitioned β literally std::ranges::is_partitioned, no cap.
β¨ Synopsis β¨
Defined in header <algorithm>. Call signature vibes:
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)
- Returns
trueif all elements in the range[first, last)that satisfy the predicatepredafter projection appear before all elements that don't. Also returnstrueif[first, last)is empty. - Same as (1), but uses
ras the source range, as if usingranges::begin(r)asfirstandranges::end(r)aslast.
The function-like entities described on this page are 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.
In practice, they may be implemented as function objects, or with special compiler extensions.
π Parameters π
first,lastβ iterator-sentinel pair denoting the range of elements to examinerβ the range of elements to examinepredβ predicate to apply to the projected elementsprojβ projection to apply to the elements
πΈ Return value πΈ
true if the range [first, last) is empty or is partitioned by pred, false otherwise.
π₯ Complexity π₯
At most ranges::distance(first, last) applications of pred and proj.
π Possible implementation π
If you're nosy, here's one possible implementation, bestie:
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));
} };
inline constexpr auto is_partitioned = is_partitioned_fn();
π Example π
// Run this code
#include <algorithm>
#include <array>
#include <iostream>
#include <numeric>
#include <utility>
int main()
{
std::array<int, 9> v;
auto print = [&v](bool o)
{
for (int x : v)
std::cout << x << ' ';
std::cout << (o ? "=> " : "=> not ") << "partitioned\n";
};
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 => not partitioned
2 4 6 8 5 3 7 1 9 => partitioned
9 1 7 3 5 8 6 4 2 => not partitioned
9 1 7 3 5 8 6 4 2 => partitioned
π¦ See also π¦
ranges::partitionβ divides a range of elements into two groups(C++20)(niebloid)ranges::partition_pointβ locates the partition point of a partitioned range(C++20)(niebloid)is_partitionedβ determines if the range is partitioned by the given predicate(C++11)(function template)
Source/version/date: http://cppreference.com Β· 2024.06.10 Β· std::ranges..._partitioned(3) β mwah, bestie π
π
π¦ 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