std::ranges::stable_partition
来自cppreference.com
定义于头文件 <algorithm>
|
||
调用签名 |
||
template<std::bidirectional_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred> |
(1) | (C++20 起) |
template<ranges::bidirectional_range R, class Proj = std::identity, std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>> Pred> |
(2) | (C++20 起) |
1) 重排序范围
[first, last)
中的元素,使得所有谓词 pred
对 proj
投影后返回 true 的元素在所有谓词 pred
对 proj
投影后返回 false 的元素之前。算法是稳定的,即保持元素的相对顺序。2) 同 (1) ,但以
r
为范围,如同以 ranges::begin(r) 为 first
并以 ranges::end(r) 为 last
。此页面上描述的仿函数实体是 niebloid ,即:
实际上,它们能以函数对象,或以某些特殊编译器扩展实现。
参数
first, last | - | 要重排序的元素范围 |
r | - | 要重排序的元素范围 |
pred | - | 应用到投影后元素的谓词 |
proj | - | 应用到元素的投影 |
返回值
1) 等于 {pivot, last} 的对象,其中
pivot
是指向第二群首元素的迭代器。复杂度
给定 N = ranges::distance(first, last) ,复杂度最坏为 N·log(N) 次交换,而在使用额外内存时仅 𝓞(N) 次交换。准确应用 N 次谓词 pred
和投影 proj
。
注解
此函数尝试分配临时缓冲区。若分配失败,则选择效率较低的算法。
可能的实现
此实现不使用额外内存缓冲区而这样可能较为低效。参阅 MSVC STL 与 libstdc++ 中的实现。
struct stable_partition_fn { 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> operator()( I first, S last, Pred pred, Proj proj = {} ) const { first = ranges::find_if_not(first, last, pred, proj); I mid = first; while (mid != last) { mid = ranges::find_if(mid, last, pred, proj); if (mid == last) break; I last2 = ranges::find_if_not(mid, last, pred, proj); ranges::rotate(first, mid, last2); first = ranges::next(first, ranges::distance(mid, last2)); mid = last2; } return {std::move(first), std::move(mid)}; } 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> operator()( R&& r, Pred pred, Proj proj = {} ) const { return (*this)(ranges::begin(r), ranges::end(r), std::move(pred), std::move(proj)); } }; inline constexpr stable_partition_fn stable_partition{}; |
示例
运行此代码
#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
参阅
(C++20) |
将范围中的元素分为二组 (niebloid) |
(C++20) |
复制一个范围,将各元素分为二组 (niebloid) |
(C++20) |
判断范围是否已按给定的谓词划分 (niebloid) |
将元素分为两组,同时保留其相对顺序 (函数模板) |