名前空間
変種
操作

std::transform_inclusive_scan

提供: 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)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
二分探索操作
集合操作 (ソート済み範囲用)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)

順列
数値演算

transform_inclusive_scan
(C++17)
C のライブラリ
 
ヘッダ <numeric> で定義
(1)
template<class InputIt, class OutputIt,

          class BinaryOperation, class UnaryOperation >
OutputIt transform_inclusive_scan( InputIt first, InputIt last, OutputIt d_first,

                                   BinaryOperation binary_op, UnaryOperation unary_op );
(C++17以上)
(C++20未満)
template<class InputIt, class OutputIt,

          class BinaryOperation, class UnaryOperation >
constexpr OutputIt transform_inclusive_scan( InputIt first, InputIt last,
                                             OutputIt d_first,
                                             BinaryOperation binary_op,

                                             UnaryOperation unary_op );
(C++20以上)
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2,

          class BinaryOperation, class UnaryOperation >
ForwardIt2 transform_inclusive_scan( ExecutionPolicy&& policy,
                                     ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,

                                     BinaryOperation binary_op, UnaryOperation unary_op );
(2) (C++17以上)
(3)
template<class InputIt, class OutputIt,

          class BinaryOperation, class UnaryOperation, class T >
OutputIt transform_inclusive_scan( InputIt first, InputIt last, OutputIt d_first,
                                   BinaryOperation binary_op, UnaryOperation unary_op,

                                   T init );
(C++17以上)
(C++20未満)
template<class InputIt, class OutputIt,

          class BinaryOperation, class UnaryOperation, class T >
constexpr OutputIt transform_inclusive_scan( InputIt first, InputIt last,
                                             OutputIt d_first,
                                             BinaryOperation binary_op,
                                             UnaryOperation unary_op,

                                             T init );
(C++20以上)
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2,

          class BinaryOperation, class UnaryOperation, class T >
ForwardIt2 transform_inclusive_scan( ExecutionPolicy&& policy,
                                     ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,
                                     BinaryOperation binary_op, UnaryOperation unary_op,

                                     T init );
(4) (C++17以上)

範囲 [first, last) 内の各要素を unary_op で変換し、その結果の範囲に対してオプションで init を初期値として用いた binary_op による inclusive prefix sum を計算し、その結果を d_first から始まる範囲に書き込みます。 「inclusive」は i 番目の入力要素が i 番目の合計に含まれるという意味です。

形式的には、以下の値を [d_first, d_first + (last - first)) 内のイテレータ i それぞれを通して代入します。

  • オーバーロード (1-2) の場合、 [first, first + (i - d_first + 1)) 内のすべての j に対する unary_op(*j)...binary_op による一般化された非可換な和。
  • オーバーロード (3-4) の場合、 [first, first + (i - d_first + 1)) 内のすべての j に対する init, unary_op(*j)...binary_op による一般化された非可換な和。

ただし、一般化された非可換な和 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)policy に従って実行されます。 また、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true でなければ、オーバーロード解決に参加しません。

unary_op および binary_op は、範囲 [first, last) または [d_first, d_first + (last - first)) 内の要素を変更してはならず、イテレータ (終端イテレータも含む) または部分範囲を無効化してはなりません。 そうでなければ、動作は未定義です。

目次

[編集]引数

first, last - 加算する要素の範囲
d_first - 結果を格納する範囲の先頭。 first と等しくても構いません
policy - 使用する実行ポリシー。 詳細は実行ポリシーを参照してください
init - 初期値
unary_op - 入力範囲の各要素に適用される単項 FunctionObject。 戻り値の型は binary_op への入力として受け付けられなければなりません
binary_op - unary_op の結果、他の binary_op の結果および init (提供された場合) に適用される二項 FunctionObject
型の要件
-
InputItLegacyInputIterator の要件を満たさなければなりません。
-
OutputItLegacyOutputIterator の要件を満たさなければなりません。
-
ForwardIt1LegacyForwardIterator の要件を満たさなければなりません。 また、 init が提供されない場合、 ForwardIt1 の値型は MoveConstructible でなければならず、 binary_op(unary_op(*first), unary_op(*first)) は ForwardIt1 の値型に変換可能でなければなりません。
-
ForwardIt2LegacyForwardIterator の要件を満たさなければなりません。
-
T (if init is provided)MoveConstructible の要件を満たさなければなりません。 binary_op(init, unary_op(*first))binary_op(init, init) および binary_op(unary_op(*first), unary_op(*first)) はすべて T に変換可能でなければなりません。

[編集]戻り値

書き込まれた最後の要素の次の要素を指すイテレータ。

[編集]計算量

それぞれ O(last - first) 回の binary_op および unary_op の適用。

[編集]例外

テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。

  • アルゴリズムの一部として呼び出された関数の実行が例外を投げ、 ExecutionPolicy標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆる ExecutionPolicy については、動作は処理系定義です。
  • アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。

[編集]ノート

unary_opinit には適用されません。


引数 init は、 std::transform_exclusive_scan と異なり、この関数の場合はオプションであるため、最後に指定します。

[編集]

#include <functional>#include <iostream>#include <iterator>#include <numeric>#include <vector>   int times_10(int x){return x *10;}   int main(){std::vector data {3, 1, 4, 1, 5, 9, 2, 6};   std::cout<<"10 times exclusive sum: ";std::transform_exclusive_scan(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, " "), 0, std::plus<int>{}, times_10);std::cout<<"\n10 times inclusive sum: "; std::transform_inclusive_scan(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, " "), std::plus<int>{}, times_10);}

出力:

10 times exclusive sum: 0 30 40 80 90 140 230 250 10 times inclusive sum: 30 40 80 90 140 230 250 310

[編集]関連項目

指定範囲の要素の部分和を計算します
(関数テンプレート)[edit]
指定範囲の要素に関数を適用し、結果を別の範囲に格納します
(関数テンプレート)[edit]
std::partial_sum と同様ですが、計算順序は不定です
(関数テンプレート)[edit]
関数オブジェクトを適用した結果に対して exclusive scan を計算します
(関数テンプレート)[edit]
close