std::is_partitioned
| ヘッダ <algorithm>
|
||
template< class InputIt, class UnaryPred >
bool is_partitioned( InputIt first, InputIt last, UnaryPred p );
|
で定義 (1) | (C++11から) (constexpr C++20から) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >
bool is_partitioned( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPred p );
|
(2) | (C++17から) |
[first, last) が述語 p によって分割されているか確認する:p を満たすすべての要素は、満たさないすべての要素より前に現れる。policy に従って実行される。true である場合にのみオーバーロード解決に参加する:
|
|
(C++20まで) |
|
|
(C++20から) |
パラメータ
| first, last | - | 検査する要素の範囲を定義するイテレータのペア |
| policy | - | 使用する実行ポリシー |
| p | - |
範囲の先頭に見つかることが期待される要素に対して
true
を返す単項述語
式
p
(
v
)
は、型(const修飾されている可能性もある)
|
| 型要件 | ||
-
InputIt
は
LegacyInputIterator
の要件を満たさなければならない。
|
||
-
ForwardIt
は
LegacyForwardIterator
の要件を満たさなければならず、その値型は
UnaryPred
のパラメータ型に変換可能でなければならない。
|
||
-
UnaryPred
は
Predicate
の要件を満たさなければならない。
|
||
戻り値
true
を返す条件は、範囲
[
first
,
last
)
の要素
e
が式
p
(
e
)
に関して
区分化
されている場合です。
false
を返すのはそれ以外の場合です。
計算量
最大で std:: distance ( first, last ) 回の p の適用。
例外
ExecutionPolicyという名前のテンプレートパラメータを持つオーバーロードは、
以下のようにエラーを報告します:
-
アルゴリズムの一部として呼び出された関数の実行が例外をスローした場合、
ExecutionPolicyが 標準ポリシー のいずれかであるとき、 std::terminate が呼び出されます。その他のExecutionPolicyについては、動作は実装定義です。 - アルゴリズムがメモリの確保に失敗した場合、 std::bad_alloc がスローされます。
実装例
template<class InputIt, class UnaryPred> bool is_partitioned(InputIt first, InputIt last, UnaryPred p) { for (; first != last; ++first) if (!p(*first)) break; for (; first != last; ++first) if (p(*first)) return false; return true; } |
例
#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.cbegin(), v.cend(), is_even) << ' '; std::cout << std::is_partitioned(v.crbegin(), v.crend(), is_even) << '\n'; }
出力:
false true false true
関連項目
| 要素の範囲を2つのグループに分割する (関数テンプレート & アルゴリズム関数オブジェクト) | |
(C++20) |
|
(C++11) |
分割された範囲の分割点を特定する (関数テンプレート & アルゴリズム関数オブジェクト) |
(C++20) |
|
(C++20) |
範囲が指定された述語で分割されているかどうかを判定する (アルゴリズム関数オブジェクト) |