std:: partition_copy
|
ヘッダーで定義
<algorithm>
|
||
|
template
<
class
InputIt,
class
OutputIt1,
class
OutputIt2,
class
UnaryPred
>
|
(1) |
(C++11以降)
(constexpr C++20以降) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2,
class
ForwardIt3,
class
UnaryPred
>
|
(2) | (C++17以降) |
[
first
,
last
)
の要素を、述語
p
が返す値に応じて2つの異なる範囲にコピーします。
- 述語 p を満たす要素は、 d_first_true から始まる範囲にコピーされます。
- 残りの要素は、 d_first_false から始まる範囲にコピーされます。
|
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
)
は、
|
| 型要件 | ||
-
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つのグループに分割する
(関数テンプレート) |
|
|
(C++11)
|
要素の範囲を新しい位置にコピーする
(関数テンプレート) |
|
特定の条件を満たす要素を除外して範囲をコピーする
(関数テンプレート) |
|
|
(C++20)
|
要素を2つのグループに分割しながらコピーする
(アルゴリズム関数オブジェクト) |