std::inclusive_scan
ヘッダ <numeric> で定義
|
||
(1) | ||
template< class InputIt, class OutputIt > OutputIt inclusive_scan( InputIt first, |
(C++17以上) (C++20未満) |
|
template< class InputIt, class OutputIt > constexpr OutputIt inclusive_scan( InputIt first, |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardtIt2 inclusive_scan( ExecutionPolicy&& policy, ForwardIt1 first, |
(2) | (C++17以上) |
(3) | ||
template< class InputIt, class OutputIt, class BinaryOperation > OutputIt inclusive_scan( InputIt first, InputIt last, |
(C++17以上) (C++20未満) |
|
template< class InputIt, class OutputIt, class BinaryOperation > constexpr OutputIt inclusive_scan( InputIt first, InputIt last, |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryOperation > |
(4) | (C++17以上) |
(5) | ||
template< class InputIt, class OutputIt, class BinaryOperation, class T > OutputIt inclusive_scan( InputIt first, InputIt last, OutputIt d_first, |
(C++17以上) (C++20未満) |
|
template< class InputIt, class OutputIt, class BinaryOperation, class T > constexpr OutputIt inclusive_scan( InputIt first, InputIt last, OutputIt d_first, |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryOperation, class T > |
(6) | (C++17以上) |
init
を初期値として使用し (提供されれば)、 binary_op
(オーバーロード (1-2) の場合は std::plus<>()) を用いて、範囲 [first, last)
に対する inclusive prefix sum を計算し、その結果を d_first
から始まる範囲に書き込みます。 「inclusive」は、 i 番目の入力要素が i 番目の合計に含まれるという意味です。
形式的には、以下の値を [d_first, d_first + (last - first)) の各イテレータ i
を通して代入します。
- オーバーロード (1-4) の場合、 [first, first + (i - d_first + 1)) 内のすべての
j
について、binary_op
を用いた一般化された可換でない合計*j...
。 - オーバーロード (5-6) の場合、 [first, first + (i - d_first + 1)) 内のすべての
j
について、binary_op
を用いた一般化された可換でない合計init, *j...
。
ただし、一般化された可換でない合計 GNSUM(op, a
1, ..., a
N) は、以下のように定義されます。
- N=1 であれば、 a
1 です。 - N > 1 であれば、 1 < K+1 = M ≤ N である任意の K について op(GNSUM(op, a
1, ..., a
K), GNSUM(op, a
M, ..., a
N)) です。
別の言い方をすると、加算操作は適当な順序で行われるかもしれず、 binary_op
が結合法則を満たさなければ、動作は非決定的となります。
オーバーロード (2,4,6) は policy
に従って実行されます。 このオーバーロードは、std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します。
binary_op
はイテレータ (終端イテレータを含む) や部分範囲を無効化してはならず、範囲 [first, last) または [d_first, d_first + (last - first)) 内の要素を変更してはなりません。 そうでなければ、動作は未定義です。
目次 |
[編集] 引数
first, last | - | 合計する要素の範囲 |
d_first | - | 結果を格納する範囲の先頭。 first と等しくても構いません
|
policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
init | - | 初期値 (任意) |
binary_op | - | 入力イテレータを逆参照した結果、他の binary_op の結果、および init (提供された場合) に適用される二項 FunctionObject
|
型の要件 | ||
-InputIt は LegacyInputIterator の要件を満たさなければなりません。
| ||
-OutputIt は LegacyOutputIterator の要件を満たさなければなりません。
| ||
-ForwardIt1 は LegacyForwardIterator の要件を満たさなければなりません。 また、 init が提供されない場合、 ForwardIt1 の値型は MoveConstructible でなければならず、 binary_op(*first, *first) は ForwardIt1 の値型に変換可能でなければなりません。
| ||
-ForwardIt2 は LegacyForwardIterator の要件を満たさなければなりません。
| ||
-T (init が提供される場合) は MoveConstructible の要件を満たさなければなりません。 binary_op(init, *first) 、 binary_op(init, init) および binary_op(*first, *first) はすべて T に変換可能でなければなりません。
|
[編集] 戻り値
最後に書き込まれた要素の次の要素を指すイテレータ。
[編集] 計算量
O(last - first) 回の二項演算の適用。
[編集] 例外
テンプレート引数 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) |
関数オブジェクトを適用した結果に対して inclusive scan を計算します (関数テンプレート) |
(C++17) |
std::inclusive_scan と同様ですが、 i 番目の合計に i 番目の要素が含まれません (関数テンプレート) |