名前空間
変種
操作

std::is_partitioned

提供: cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムと範囲に対するアルゴリズム (C++20)
コンセプトとユーティリティ: std::sortable, std::projected, ...
制約付きアルゴリズム: std::ranges::copy, std::ranges::sort, ...
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
未初期化記憶域の操作
分割操作
is_partitioned
(C++11)
ソート操作
(C++11)
二分探索操作
集合操作 (ソート済み範囲用)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)

順列
数値演算
C のライブラリ
 
ヘッダ <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);
}

出力:

false true false

[編集] 関連項目

指定範囲の要素を2つのグループに分割します
(関数テンプレート) [edit]
分割された範囲の分割点を探します
(関数テンプレート) [edit]