std::is_partitioned
提供: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| ヘッダ <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 > constexpr bool 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 を返す単項述語。 式 |
| 型の要件 | ||
-InputIt は LegacyInputIterator の要件を満たさなければなりません。
| ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。 また、その値型が UnaryPredicate's の引数型に変換可能でなければなりません。
| ||
-UnaryPredicate は Predicate の要件を満たさなければなりません。
| ||
戻り値
範囲 [first, last) が空または p によって分割されている場合は true、そうでなければ false。
計算量
多くとも std::distance(first, last) 回の p の適用。
例外
テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicyが標準のポリシーのいずれかの場合は、 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))
return false;
return true;
}
|
例
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) |
分割された範囲の分割点を探します (関数テンプレート) |