std::partition
提供: cppreference.com
| ヘッダ <algorithm> で定義
|
||
| (1) | ||
| template< class BidirIt, class UnaryPredicate > BidirIt partition( BidirIt first, BidirIt last, UnaryPredicate p ); |
(C++11以前) | |
| template< class ForwardIt, class UnaryPredicate > ForwardIt partition( ForwardIt first, ForwardIt last, UnaryPredicate p ); |
(C++11およびそれ以降) | |
| template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate > ForwardIt partition( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p ); |
(2) | (C++17およびそれ以降) |
1) 述語
p が true を返すすべての要素が述語 p が false を返すすべての要素より前に来るように、範囲 [first, last) の要素を並べ替えます。 要素の相対順序は維持されません。2) (1) と同じですが、
policy に従って実行されます。 このオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true でなければ、オーバーロード解決に参加しません。目次 |
[編集] 引数
| first, last | - | 並べ替える要素の範囲 |
| policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
| p | - | 要素が他の要素より前に並べられるべき場合に true を返す単項述語。 述語関数のシグネチャは以下と同等なものであるべきです。 bool pred(const Type &a); シグネチャが const & を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはなりません。 |
| 型の要件 | ||
-BidirIt は BidirectionalIterator の要件を満たさなければなりません。
| ||
-ForwardIt は ValueSwappable および ForwardIterator の要件を満たさなければなりません。 ForwardIt が BidirectionalIterator の要件も満たす場合、操作はより効率よくなります。
| ||
-UnaryPredicate は Predicate の要件を満たさなければなりません。
| ||
[編集] 戻り値
2つめのグループの最初の要素を指すイテレータ。
[編集] 計算量
N = std::distance(first,last) とすると、
1) ちょうど N 回の述語の適用。
ForwardIt が BidirectionalIterator の要件を満たす場合は多くとも N/2 回の swap、そうでなければ多くとも N 回の swap。2)
O(N log N) 回の swap および O(N) 回の述語の適用。[編集] 例外
テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicyが3つの標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicyについては、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
[編集] 実装例
template<class ForwardIt, class UnaryPredicate> ForwardIt partition(ForwardIt first, ForwardIt last, UnaryPredicate p) { first = std::find_if_not(first, last, p); if (first == last) return first; for (ForwardIt i = std::next(first); i != last; ++i) { if (p(*i)) { std::iter_swap(i, first); ++first; } } return first; } |
[編集] 例
Run this code
#include <algorithm> #include <iostream> #include <iterator> #include <vector> #include <forward_list> template <class ForwardIt> void quicksort(ForwardIt first, ForwardIt last) { if(first == last) return; auto pivot = *std::next(first, std::distance(first,last)/2); ForwardIt middle1 = std::partition(first, last, [pivot](const auto& em){ return em < pivot; }); ForwardIt middle2 = std::partition(middle1, last, [pivot](const auto& em){ return !(pivot < em); }); quicksort(first, middle1); quicksort(middle2, last); } int main() { std::vector<int> v = {0,1,2,3,4,5,6,7,8,9}; std::cout << "Original vector:\n "; for(int elem : v) std::cout << elem << ' '; auto it = std::partition(v.begin(), v.end(), [](int i){return i % 2 == 0;}); std::cout << "\nPartitioned vector:\n "; std::copy(std::begin(v), it, std::ostream_iterator<int>(std::cout, " ")); std::cout << " * "; std::copy(it, std::end(v), std::ostream_iterator<int>(std::cout, " ")); std::forward_list<int> fl = {1, 30, -4, 3, 5, -4, 1, 6, -8, 2, -5, 64, 1, 92}; std::cout << "\nUnsorted list:\n "; for(int n : fl) std::cout << n << ' '; std::cout << '\n'; quicksort(std::begin(fl), std::end(fl)); std::cout << "Sorted using quicksort:\n "; for(int fi : fl) std::cout << fi << ' '; 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
Sorted using quicksort:
-8 -5 -4 -4 1 1 1 2 3 5 6 30 64 92[編集] 関連項目
| (C++11) |
指定範囲が指定した述語で分割されているかどうか調べます (関数テンプレート) |
| 相対的な順序を維持しながら要素を2つのグループに分割します (関数テンプレート) |