Namespaces
Variants

std:: transform_inclusive_scan

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
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
transform_inclusive_scan
(C++17)

Operations on uninitialized memory
ヘッダー <numeric> で定義
template < class InputIt, class OutputIt,

class BinaryOp, class UnaryOp >
OutputIt transform_inclusive_scan
( InputIt first, InputIt last, OutputIt d_first,

BinaryOp binary_op, UnaryOp unary_op ) ;
(1) (C++17以降)
(constexpr C++20以降)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2,
class BinaryOp, class UnaryOp >
ForwardIt2 transform_inclusive_scan
( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,

BinaryOp binary_op, UnaryOp unary_op ) ;
(2) (C++17以降)
template < class InputIt, class OutputIt,

class BinaryOp, class UnaryOp, class T >
OutputIt transform_inclusive_scan
( InputIt first, InputIt last, OutputIt d_first,

BinaryOp binary_op, UnaryOp unary_op, T init ) ;
(3) (C++17以降)
(constexpr C++20以降)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2,
class BinaryOp, class UnaryOp, class T >
ForwardIt2 transform_inclusive_scan
( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,

BinaryOp binary_op, UnaryOp unary_op, T init ) ;
(4) (C++17以降)
1) op を使用して包括的なプレフィックス和を計算します。
各整数 i について [ 0 , std:: distance ( first, last ) ) の範囲で、以下の操作を順次実行します:
  1. [ first , iter ] の要素を unary_op で変換した値から成るシーケンスを順序通りに生成します。ここで iter first から i 番目 の次のイテレータです。
  2. シーケンスの一般化された非可換和を binary_op を用いて計算します。
  3. 結果を * dest に代入します。ここで dest d_first から i 番目 の次のイテレータです。
3) (1) と同じですが、作成される各シーケンスは init の後に [ first , iter ] の要素が順に続く形で構成されます。
2,4) (1,3) と同様ですが、 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以降)

要素のシーケンスに対する二項演算 binary_op に関する 一般化非可換和 は以下のように定義されます:

  • シーケンスに要素が1つしかない場合、合計はその要素の値です。
  • それ以外の場合、以下の操作を順番に実行します:
  1. シーケンスから任意の2つの隣接要素 elem1 elem2 を選択します。
  2. binary_op ( elem1, elem2 ) を計算し、シーケンス内の2つの要素を結果で置き換えます。
  3. シーケンスに要素が1つだけになるまで、ステップ1と2を繰り返します。


binary_op が結合的でない場合(浮動小数点の加算など)、結果は非決定的になります。

オーバーロード (1,2) の場合、 binary_op ( unary_op ( * first ) , unary_op ( * first ) ) value type に変換できない場合、プログラムは不適格となる。 decltype ( first )

注記: - HTMLタグと属性はそのまま保持 - ` `、`
`、``タグ内のテキストは翻訳せず(この例では``内のC++コード)
- C++専門用語(binary_op、unary_op、first、decltype、value typeなど)は翻訳せず
- 技術文書としての正確性と専門性を維持

オーバーロード (3,4) について、以下のいずれかの値が T に変換可能でない場合、プログラムは不適格です:

  • binary_op ( init, init )
  • binary_op ( init, unary_op ( * first ) )
  • binary_op ( unary_op ( * first ) , unary_op ( * first ) )
(注:指定された条件により、HTMLタグ・属性、 タグ内のテキスト、C++固有用語は翻訳対象外となっています。上記のコード部分は全て翻訳せずにそのまま保持されています)

以下のいずれかの条件が満たされる場合、動作は未定義です:

  • オーバーロード (1,2) について、 decltype ( first ) の値型は MoveConstructible ではありません。
  • オーバーロード (3,4) について、 T MoveConstructible ではありません。
  • unary_op または binary_op [ first , last ) の任意の要素を変更する場合。
  • unary_op または binary_op [ first , last ] の任意のイテレータまたは部分範囲を無効化する場合。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - C++関連の専門用語(Parameters, Return value, Complexity, Exceptions, Notes, Example, See also)は原文のまま保持しました - HTMLタグ、属性、クラス名、ID、リンク先は一切変更していません - 数値や書式設定は完全に保持されています

パラメータ

first, last - 合計する要素の範囲を定義するイテレータのペア
d_first - 出力先範囲の先頭; first と等しくてもよい
policy - 使用する実行ポリシー
init - 初期値
unary_op - 入力範囲の各要素に適用される単項関数オブジェクト。戻り値の型は binary_op への入力として受け入れ可能でなければならない
binary_op - unary_op の結果、他の binary_op の結果、および提供されている場合は init に適用される二項関数オブジェクト
型要件
-
InputIt LegacyInputIterator の要件を満たさなければならない
-
OutputIt LegacyOutputIterator の要件を満たさなければならない
-
ForwardIt1, ForwardIt2 LegacyForwardIterator の要件を満たさなければならない

戻り値

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

計算量

与えられた N std:: distance ( first, last ) として:

1-4) O(N) 回の unary_op および binary_op の適用。

例外

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

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

注記

unary_op init に適用されることはありません。

パラメータ init は最後に出現します。これは std::transform_exclusive_scan とは異なります。なぜなら、この関数ではオプションであるためです。

#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
int main()
{
    std::vector data{3, 1, 4, 1, 5, 9, 2, 6};
    auto times_10 = [](int x) { return x * 10; };
    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);
    std::cout << '\n';
}

出力:

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

関連項目

要素範囲の部分和を計算する
(関数テンプレート)
要素範囲に関数を適用し、結果を出力範囲に格納する
(関数テンプレート)
std::partial_sum と同様だが、 i th 番目の入力要素を i th 番目の和に含める
(関数テンプレート)
呼び出し可能オブジェクトを適用した後、排他的スキャンを計算する
(関数テンプレート)