◐ Shell
clean mode source ↗

std::is_partitioned - cppreference.com

提供: 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 を返す単項述語。

p(v)VT 型 (およびその const 修飾された型) のすべての引数 v について、その値カテゴリにかかわらず、 bool に変換可能でなければなりません。 ただし VTInputIt の値型です。 また、 v を変更してはなりません。 そのため、引数の型 VT& は許されません。 また、 VT に対してムーブがコピーと同等でなければ VT も許されません。 (C++11以上) ​​

型の要件
-InputItLegacyInputIterator の要件を満たさなければなりません。
-ForwardItLegacyForwardIterator の要件を満たさなければなりません。 また、その値型が UnaryPredicate's の引数型に変換可能でなければなりません。
-UnaryPredicatePredicate の要件を満たさなければなりません。

戻り値

範囲 [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;
}

#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);
}

出力:

関連項目