std::ranges::partition
來自 cppreference.com
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
template< std::permutable I, std::sentinel_for<I> S, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred > |
(1) | (C++20 起) |
template< ranges::forward_range R, class Proj = std::identity, std::indirect_unary_predicate< |
(2) | (C++20 起) |
1) 重新排列範圍
[
first,
last)
中的元素,使得謂詞 pred 返回 true 的所有元素的投影 proj 排在謂詞 pred 返回 false 的元素的投影 proj 之前。不保留元素的相對順序。本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
first, last | - | 定義要重新排列的元素的範圍的迭代器-哨兵對 |
r | - | 要重新排列的元素範圍 |
pred | - | 應用於投影元素的謂詞 |
proj | - | 應用於元素的投影 |
[編輯] 返回值
一個子範圍,其起始迭代器指向第二組的第一個元素,結束迭代器等於 last。(2) 若 r 是非借用範圍
型別的右值,則返回 std::ranges::dangling。
[編輯] 複雜度
給定 N = ranges::distance(first, last),精確地呼叫謂詞和投影 N 次。如果 I
建模 ranges::bidirectional_iterator,則最多 N / 2 次交換;否則最多 N 次交換。
[編輯] 可能的實現
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; |
[編輯] 示例
執行此程式碼
#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'; }
可能的輸出
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
[編輯] 參閱
(C++20) |
複製一個範圍,並將元素分成兩組 (演算法函式物件) |
(C++20) |
判斷一個範圍是否按給定謂詞劃分 (演算法函式物件) |
(C++20) |
將元素分成兩組,同時保留它們的相對順序 (演算法函式物件) |
將一個範圍的元素分成兩組 (函式模板) |