std::exclusive_scan
| ヘッダー <numeric> で定義 |
||
| template< class InputIt, class OutputIt, class T > OutputIt exclusive_scan( InputIt first, InputIt last, |
(1) | (C++17以降) (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T > |
(2) | (C++17以降) |
| template< class InputIt, class OutputIt, class T, class BinaryOp > |
(3) | (C++17以降) (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, |
(4) | (C++17以降) |
[0, std::distance(first, last)) の各整数 i に対して、次の操作を順に実行します。- init と、その後に続く first からの i 番目のイテレータである iter までの
[first,iter)の要素で構成されるシーケンスを作成する。 - op を介したシーケンスの一般化された非可換和を計算する。
- d_first の i 番目のイテレータである dest に結果を代入します。
|
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以降) |
二項演算 binary_op を介した要素シーケンスの一般化された非可換和は、次のように定義される。
- シーケンスに1つの要素しかない場合、合計はその要素の値です。
- そうでない場合、次の操作を順に実行します。
- シーケンスから任意の2つの隣接する要素 elem1 と elem2 を選択します。
- binary_op(elem1, elem2) を計算し、シーケンス内の2つの要素を結果で置き換えます。
- シーケンスに1つの要素だけが残るまでステップ1と2を繰り返します。
実際のバイナリ操作として binary_op が与えられた場合
- binary_op が結合法則に従わない場合 (浮動小数点数の加算など)、結果は非決定論的です。
- 以下のいずれかの値が
Tに変換できない場合、プログラムは不正となる
- binary_op(init, *first)
- binary_op(init, init)
- binary_op(*first, *first)
- 次のいずれかの条件が満たされる場合、動作は未定義です。
-
Tは MoveConstructible ではない。 - binary_op が
[first,last)のいずれかの要素を変更する。 - binary_op が
[first,last]の任意のイテレータまたはサブレンジを無効にする。
-
目次 |
[編集] パラメーター
| first, last | - | 合計する要素の範囲を定義するイテレーターのペア |
| d_first | - | デスティネーション範囲の先頭。first と同じでもよい |
| policy | - | 使用する 実行ポリシー |
| init | - | 初期値 |
| op | - | 入力イテレーターの参照外し結果、他の op の結果、および init に適用される二項FunctionObject |
| 型要件 | ||
-InputIt は LegacyInputIterator の要件を満たす必要があります。 | ||
-OutputIt は LegacyOutputIterator の要件を満たさなければなりません。 | ||
-ForwardIt1, ForwardIt2 は LegacyForwardIterator の要件を満たさなければなりません。 | ||
[編集] 戻り値
最後に書き込まれた要素の次を指すイテレータ。
[編集] 計算量
std::distance(first, last) を N とする
[編集] 例外
ExecutionPolicy というテンプレートパラメータを持つオーバーロードは、次のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外をスローし、
ExecutionPolicyが 標準ポリシー のいずれかである場合、std::terminate が呼び出されます。その他のExecutionPolicyの場合、動作は実装定義です。 - アルゴリズムがメモリの割り当てに失敗した場合、std::bad_alloc がスローされます。
[編集] 例
#include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> int main() { std::vector data{3, 1, 4, 1, 5, 9, 2, 6}; std::cout << "Exclusive sum: "; std::exclusive_scan(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, " "), 0); std::cout << "\nInclusive sum: "; std::inclusive_scan(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << "\n\nExclusive product: "; std::exclusive_scan(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, " "), 1, std::multiplies<>{}); std::cout << "\nInclusive product: "; std::inclusive_scan(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, " "), std::multiplies<>{}); }
出力
Exclusive sum: 0 3 4 8 9 14 23 25 Inclusive sum: 3 4 8 9 14 23 25 31 Exclusive product: 1 3 3 12 12 60 540 1080 Inclusive product: 3 3 12 12 60 540 1080 6480
[編集] 関連項目
| 範囲内の隣接する要素間の差を計算する (関数テンプレート) | |
| 範囲の要素を合計または畳み込む (関数テンプレート) | |
| 範囲の要素の部分和を計算する (関数テンプレート) | |
| (C++17) |
呼び出し可能オブジェクトを適用し、エクスクルーシブスキャンを計算する (関数テンプレート) |
| (C++17) |
std::partial_sum に似ているが、i 番目の入力要素を i 番目の合計に含める (関数テンプレート) |