std::is_partitioned
提供: cppreference.com
ヘッダ <algorithm> で定義 | ||
(1) | ||
template<class InputIt, class UnaryPredicate > bool is_partitioned( InputIt first, InputIt last, UnaryPredicate p ); | (C++11およびそれ以降) (C++20以前) | |
template<class InputIt, class UnaryPredicate > constexprbool is_partitioned( InputIt first, InputIt last, UnaryPredicate p ); | (C++20およびそれ以降) | |
template<class ExecutionPolicy, class ForwardIt, class UnaryPredicate > bool is_partitioned( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p ); | (2) | (C++17およびそれ以降) |
1) 範囲
[first, last)
内の述語 p
を満たすすべての要素が満たさないすべての要素より前に現れる場合は true を返します。 [first, last)
が空の場合も true を返します。2)(1) と同じですが、
policy
に従って実行されます。 このオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します目次 |
[編集]引数
first, last | - | 調べる要素の範囲 |
policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
p | - | 範囲の先頭に見つかることが期待される要素に対して true を返す単項述語。 述語関数のシグネチャは以下と同等なものであるべきです。 bool pred(const Type &a); シグネチャが const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはなりません。 |
型の要件 | ||
-InputIt は InputIterator の要件を満たさなければなりません。 | ||
-ForwardIt は ForwardIterator の要件を満たさなければなりません。 また、その値型が UnaryPredicate's の引数型に変換可能でなければなりません。 | ||
-UnaryPredicate は Predicate の要件を満たさなければなりません。 |
[編集]戻り値
範囲 [first, last)
が空または p
によって分割されている場合は true、そうでなければ false。
[編集]計算量
多くとも std::distance(first, last)
回の p
の適用。
[編集]例外
テンプレート引数 ExecutionPolicy
を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicy
が3つの標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicy
については、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
[編集]実装例
template<class InputIt, class UnaryPredicate >bool is_partitioned(InputIt first, InputIt last, UnaryPredicate p){for(; first != last;++first)if(!p(*first))break;for(; first != last;++first)if(p(*first))returnfalse;returntrue;} |
[編集]例
Run this code
#include <algorithm>#include <array>#include <iostream> int main(){std::array<int, 9> v ={1, 2, 3, 4, 5, 6, 7, 8, 9}; auto is_even =[](int i){return i %2==0;};std::cout.setf(std::ios_base::boolalpha);std::cout<< std::is_partitioned(v.begin(), v.end(), is_even)<<' '; std::partition(v.begin(), v.end(), is_even);std::cout<< std::is_partitioned(v.begin(), v.end(), is_even)<<' '; std::reverse(v.begin(), v.end());std::cout<< std::is_partitioned(v.begin(), v.end(), is_even);}
出力:
false true false
[編集]関連項目
指定範囲の要素を2つのグループに分割します (関数テンプレート) | |
(C++11) | 分割された範囲の分割点を探します (関数テンプレート) |