std::ranges::partition(3)
π std::ranges::partition(3)
okay so bestie, welcome to the glow-up era of <algorithm> β this is the std::ranges::partition page, straight from the C++ Standard Libary docs (yes, "Libary," they spelled it like that, we're not fixing it, we're respecting the lore π). Anyway. It's giving main character energy and you're gonna love it.
π NAME π
std::ranges::partition β std::ranges::partition.
That's it. That's the whole name. The manpage literally just said the name twice, so I'm saying it twice too. Periodt. β¨
π¦ SYNOPSIS π¦
Defined in header <algorithm>.
Call signature:
// (1) (since C++20)
template< std::permutable I, std::sentinel_for<I> S, class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
constexpr ranges::subrange<I>
partition( I first, S last, Pred pred, Proj proj = {} );
// (2) (since C++20)
template< ranges::forward_range R, class Proj = std::identity,
std::indirect_unary_predicate<
std::projected<ranges::iterator_t<R>, Proj>> Pred >
requires std::permutable<ranges::iterator_t<R>>
constexpr ranges::borrowed_subrange_t<R>
partition( R&& r, Pred pred, Proj proj = {} );
okay so what do the two versions actually do, because you deserve to know:
- It reorders the elements in the range
[first, last)in such a way that the projectionprojof all elements for which the predicatepredreturnstrueprecede the projectionprojof elements for which predicatepredreturnsfalse. Relative order of elements is not preserved. Like, she reorders, she does not preserve. Do not come for her about it. - Same as (1), but uses
ras the source range, as if usingranges::begin(r)asfirstandranges::end(r)aslast.
Now β 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. π§ iykyk.
πΈ PARAMETERS πΈ
first,lastβ the range of elements to reorderrβ the range of elements to reorderpredβ predicate to apply to the projected elementsprojβ projection to apply to the elements
π RETURN VALUE π
A subrange starting with an iterator to the first element of the second group and finishing with an iterator equal to last.
And heads up: (2) returns std::ranges::dangling if r is an rvalue of non-borrowed_range type. π Don't get caught lacking.
β COMPLEXITY β
Given N = ranges::distance(first, last):
- exactly
Napplications of the predicate and projection - at most
N / 2swaps ifImodelsranges::bidirectional_iterator - at most
Nswaps otherwise
Efficient queen behavior. We love to see it. π
π«Ά POSSIBLE IMPLEMENTATION π«Ά
struct partition_fn
{
template<std::permutable I, std::sentinel_for<I> S, class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
constexpr ranges::subrange<I>
operator()(I first, S last, Pred pred, Proj proj = {}) const
{
first = ranges::find_if_not(first, last, std::ref(pred), std::ref(proj));
if (first == last)
return {first, first};
for (auto i = ranges::next(first); i != last; ++i)
{
if (std::invoke(pred, std::invoke(proj, *i)))
{
ranges::iter_swap(i, first);
++first;
}
}
return {std::move(first), std::move(last)};
}
template<ranges::forward_range R, class Proj = std::identity,
std::indirect_unary_predicate<
std::projected<ranges::iterator_t<R>, Proj>> Pred>
requires std::permutable<ranges::iterator_t<R>>
constexpr ranges::borrowed_subrange_t<R>
operator()(R&& r, Pred pred, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r),
std::ref(pred), std::ref(proj));
}
};
inline constexpr partition_fn partition;
no cap, that's the whole vibe right there. π
π EXAMPLE π
// Run this code
#include <algorithm>
#include <forward_list>
#include <functional>
#include <iostream>
#include <iterator>
#include <ranges>
#include <vector>
namespace ranges = std::ranges;
template<class I, std::sentinel_for<I> S, class Cmp = ranges::less>
requires std::sortable<I, Cmp>
void quicksort(I first, S last, Cmp cmp = Cmp {})
{
using reference = std::iter_reference_t<I>;
if (first == last)
return;
auto size = ranges::distance(first, last);
auto pivot = ranges::next(first, size - 1);
ranges::iter_swap(pivot, ranges::next(first, size / 2));
auto tail = ranges::partition(first, pivot, [=](reference em)
{
return std::invoke(cmp, em, *pivot); // em < pivot
});
ranges::iter_swap(pivot, tail.begin());
quicksort(first, tail.begin(), std::ref(cmp));
quicksort(ranges::next(tail.begin()), last, std::ref(cmp));
}
int main()
{
std::ostream_iterator<int> cout {std::cout, " "};
std::vector<int> v {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::cout << "Original vector: \t";
ranges::copy(v, cout);
auto tail = ranges::partition(v, [](int i) { return i % 2 == 0; });
std::cout << "\nPartitioned vector: \t";
ranges::copy(ranges::begin(v), ranges::begin(tail), cout);
std::cout << "β ";
ranges::copy(tail, cout);
std::forward_list<int> fl {1, 30, -4, 3, 5, -4, 1, 6, -8, 2, -5, 64, 1, 92};
std::cout << "\nUnsorted list: \t\t";
ranges::copy(fl, cout);
quicksort(ranges::begin(fl), ranges::end(fl), ranges::greater {});
std::cout << "\nQuick-sorted list: \t";
ranges::copy(fl, cout);
std::cout << '\n';
}
π Possible output π
Original vector: 0 1 2 3 4 5 6 7 8 9
Partitioned vector: 0 8 2 6 4 β 5 3 7 1 9
Unsorted list: 1 30 -4 3 5 -4 1 6 -8 2 -5 64 1 92
Quick-sorted list: 92 64 30 6 5 3 2 1 1 1 -4 -4 -5 -8
look at that β doing its little separator moment. ate. π₯
π§ SEE ALSO π§
ranges::partition_copyβ copies a range dividing the elements into two groups (C++20) (niebloid)ranges::is_partitionedβ determines if the range is partitioned by the given predicate (C++20) (niebloid)ranges::stable_partitionβ divides elements into two groups while preserving their relative order (C++20) (niebloid)partitionβ divides a range of elements into two groups (function template)
bestie reminder: this one does not preserve relative order. if you need the order kept, that's ranges::stable_partition's whole thing. choose wisely π
mwah β sourced from http://cppreference.com, version dated 2024.06.10, page std::ranges::partition(3). go partition something iconic today πβ¨
π¦ 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