Namespaces
Variants

std:: partition_copy

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
partition_copy
(C++11)
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
ヘッダーで定義 <algorithm>
template < class InputIt, class OutputIt1,

class OutputIt2, class UnaryPred >
std:: pair < OutputIt1, OutputIt2 >
partition_copy ( InputIt first, InputIt last,
OutputIt1 d_first_true, OutputIt2 d_first_false,

UnaryPred p ) ;
(1) (C++11以降)
(constexpr C++20以降)
template < class ExecutionPolicy, class ForwardIt1, class ForwardIt2,

class ForwardIt3, class UnaryPred >
std:: pair < ForwardIt2, ForwardIt3 >
partition_copy ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first_true, ForwardIt3 d_first_false,

UnaryPred p ) ;
(2) (C++17以降)
1) 範囲 [ first , last ) の要素を、述語 p が返す値に応じて2つの異なる範囲にコピーします。
  • 述語 p を満たす要素は、 d_first_true から始まる範囲にコピーされます。
  • 残りの要素は、 d_first_false から始まる範囲にコピーされます。
2) (1) と同様ですが、 policy に従って実行されます。
このオーバーロードは、以下の条件がすべて満たされる場合にのみオーバーロード解決に参加します:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true であること。

(C++20まで)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true であること。

(C++20以降)

* first d_first_true または d_first_false に対して writable でない場合、プログラムは不適格です。

入力範囲と2つの出力範囲のうち、いずれか2つの範囲が重複している場合、動作は未定義です。

目次

パラメータ

first, last - コピー元の要素のソース 範囲 を定義するイテレータのペア
d_first_true - p を満たす要素の出力範囲の先頭
d_first_false - p を満たさない要素の出力範囲の先頭
policy - 使用する 実行ポリシー
p - 要素がd_first_trueに配置されるべき場合に​ true を返す単項述語

p ( v ) は、 VT 型(const修飾されている可能性もある)のすべての引数 v に対して bool に変換可能でなければならず、 値カテゴリ に関わらず、 v を変更してはならない。したがって、 VT & のパラメータ型は許可されない 。また、 VT に対するムーブがコピーと等価でない限り、 VT も許可されない (C++11以降) 。 ​

型要件
-
InputIt LegacyInputIterator の要件を満たさなければならない
-
OutputIt1, OutputIt2 LegacyOutputIterator の要件を満たさなければならない
-
ForwardIt1, ForwardIt2, ForwardIt3 LegacyForwardIterator の要件を満たさなければならない
-
UnaryPred Predicate の要件を満たさなければならない

戻り値

std::pair の終端イテレータと d_first_true 範囲の終端イテレータから構築された d_first_false 範囲の終端イテレータから構築されたペア。

計算量

std:: distance ( first, last ) 回だけ正確に p を適用します。

オーバーロード (2) の場合、 ForwardIt の値型が CopyConstructible でない場合、パフォーマンスコストが発生する可能性があります。

例外

テンプレートパラメータ ExecutionPolicy を持つオーバーロードは、 以下のようにエラーを報告します:

  • アルゴリズムの一部として呼び出された関数の実行が例外をスローした場合、 ExecutionPolicy 標準ポリシー のいずれかであるとき、 std::terminate が呼び出される。その他の ExecutionPolicy については、動作は実装定義である。
  • アルゴリズムがメモリの確保に失敗した場合、 std::bad_alloc がスローされる。

実装例

partition_copy (1)
template<class InputIt, class OutputIt1,
         class OutputIt2, class UnaryPred>
constexpr //< since C++20
std::pair<OutputIt1, OutputIt2>
    partition_copy(InputIt first, InputIt last,
                   OutputIt1 d_first_true, OutputIt2 d_first_false,
                   UnaryPred p)
{
    for (; first != last; ++first)
    {
        if (p(*first))
        {
            *d_first_true = *first;
            ++d_first_true;
        }
        else
        {
            *d_first_false = *first;
            ++d_first_false;
        }
    }
    return std::pair<OutputIt1, OutputIt2>(d_first_true, d_first_false);
}

#include <algorithm>
#include <iostream>
#include <utility>
void print(auto rem, const auto& v)
{
    for (std::cout << rem; const auto& x : v)
        std::cout << x << ' ';
    std::cout << '\n';
}
int main()
{
    int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int true_arr[5] = {0};
    int false_arr[5] = {0};
    std::partition_copy(std::begin(arr), std::end(arr),
                        std::begin(true_arr), std::begin(false_arr),
                        [](int i) { return 4 < i; });
    print("true_arr:  ", true_arr);
    print("false_arr: ", false_arr);
}

出力:

true_arr:  5 6 7 8 9
false_arr: 0 1 2 3 4

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
P0896R4 C++11
C++17
1. InputIt (C++11)/ ForwardIt1 (C++17)の値型は
CopyAssignable であることが要求されていた
2. 2つの出力範囲が重複する可能性があった
1. 要求されない
2. この場合の動作は
未定義である

関連項目

要素の範囲を2つのグループに分割する
(関数テンプレート)
要素の相対的な順序を保持しながら2つのグループに分割する
(関数テンプレート)
要素の範囲を新しい位置にコピーする
(関数テンプレート)
特定の条件を満たす要素を除外して範囲をコピーする
(関数テンプレート)
要素を2つのグループに分割しながらコピーする
(アルゴリズム関数オブジェクト)