std::ranges::stable_partition() algorithm
- since C++20
- since C++26
- Simplified
- Detailed
// (1)
ranges::subrange<I>
stable_partition( I first, S last, Pred pred, Proj proj = {} );
// (2)
ranges::borrowed_subrange_t<R>
stable_partition( R&& r, Pred pred, Proj proj = {} );
The type of arguments are generic and have the following constraints:
I-std::bidirectional_iteratorS-std::sentinel_for<I>R-std::ranges::bidirectional_rangePred:- (1) -
std::indirect_unary_predicate<std::projected<I, Proj>> - (2) -
std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>>
- (1) -
Proj- (none)
The Proj template argument has the following default type std::identity for all overloads.
Additionally, each overload has the following constraints:
- (1) -
std::permutable<I> - (2) -
std::permutable<ranges::iterator_t<R>>
// (1)
template<
std::bidirectional_iterator I,
std::sentinel_for<I> S,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred
>
requires std::permutable<I>
ranges::subrange<I>
stable_partition( I first, S last, Pred pred, Proj proj = {} );
// (2)
template<
ranges::bidirectional_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>>
ranges::borrowed_subrange_t<R>
stable_partition( R&& r, Pred pred, Proj proj = {} );
- Simplified
- Detailed
// (1)
constexpr ranges::subrange<I>
stable_partition( I first, S last, Pred pred, Proj proj = {} );
// (2)
constexpr ranges::borrowed_subrange_t<R>
stable_partition( R&& r, Pred pred, Proj proj = {} );
The type of arguments are generic and have the following constraints:
I-std::bidirectional_iteratorS-std::sentinel_for<I>R-std::ranges::bidirectional_rangePred:- (1) -
std::indirect_unary_predicate<std::projected<I, Proj>> - (2) -
std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>>
- (1) -
Proj- (none)
The Proj template argument has the following default type std::identity for all overloads.
Additionally, each overload has the following constraints:
- (1) -
std::permutable<I> - (2) -
std::permutable<ranges::iterator_t<R>>
// (1)
template<
std::bidirectional_iterator I,
std::sentinel_for<I> S,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred
>
requires std::permutable<I>
constexpr ranges::subrange<I>
stable_partition( I first, S last, Pred pred, Proj proj = {} );
// (2)
template<
ranges::bidirectional_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>
stable_partition( R&& r, Pred pred, Proj proj = {} );
-
(1) 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.The algorithms is stable, the relative order of elements is preserved.
-
(2) Same as (1), but uses
ras the range, as if usingranges::begin(r)asfirstandranges::end(r)aslast.
The function-like entities described on this page are niebloids.
Parameters
first last | The range of elements to reorder. |
r | The range of elements to reorder. |
pred | The predicate to apply to the projected elements. |
proj | The projection to apply to the elements. |
Return value
An object equal to
{
pivot,
last
}
Where pivot is an iterator to the first element of the second group.
- (2) Same as (1) if
ris an lvalue or of aborrowed_rangetype. Otherwise returnsstd::ranges::dangling.
Complexity
Given N ranges::distance(first, last):
The complexity is at worst N * log(N) swaps, and only O(N) swaps in case an extra memory buffer is used.
Exactly N applications of the predicate pred and projection proj.
Exceptions
(none)
Possible implementation
stable_partition(1) and stable_partition(2)
Notes
This function attempts to allocate a temporary buffer. If the allocation fails, the less efficient algorithm is chosen.
Examples
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
namespace rng = std::ranges;
template<std::permutable I, std::sentinel_for<I> S>
constexpr void stable_sort(I first, S last)
{
if (first == last)
return;
auto pivot = *rng::next(first, rng::distance(first, last) / 2, last);
auto left = [pivot](const auto& em) { return em < pivot; };
auto tail1 = rng::stable_partition(first, last, left);
auto right = [pivot](const auto& em) { return !(pivot < em); };
auto tail2 = rng::stable_partition(tail1, right);
stable_sort(first, tail1.begin());
stable_sort(tail2.begin(), tail2.end());
}
void print(const auto rem, auto first, auto last, bool end = true)
{
std::cout << rem;
for (; first != last; ++first)
std::cout << *first << ' ';
std::cout << (end ? "\n" : "");
}
int main()
{
const auto original = {9, 6, 5, 2, 3, 1, 7, 8};
std::vector<int> vi {};
auto even = [](int x) { return 0 == (x % 2); };
print("Original vector:\t", original.begin(), original.end(), "\n");
vi = original;
const auto ret1 = rng::stable_partition(vi, even);
print("Stable partitioned:\t", vi.begin(), ret1.begin(), 0);
print("│ ", ret1.begin(), ret1.end());
vi = original;
const auto ret2 = rng::partition(vi, even);
print("Partitioned:\t\t", vi.begin(), ret2.begin(), 0);
print("│ ", ret2.begin(), ret2.end());
vi = {16, 30, 44, 30, 15, 24, 10, 18, 12, 35};
print("Unsorted vector: ", vi.begin(), vi.end());
stable_sort(rng::begin(vi), rng::end(vi));
print("Sorted vector: ", vi.begin(), vi.end());
}
Original vector: 9 6 5 2 3 1 7 8
Stable partitioned: 6 2 8 │ 9 5 3 1 7
Partitioned: 8 6 2 │ 5 3 1 7 9
Unsorted vector: 16 30 44 30 15 24 10 18 12 35
Sorted vector: 10 12 15 16 18 24 30 30 35 44
Hover to see the original license.