Namespaces
Variants

std:: exclusive_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
Operations on uninitialized memory
ヘッダー <numeric> で定義
template < class InputIt, class OutputIt, class T >

OutputIt exclusive_scan ( InputIt first, InputIt last,

OutputIt d_first, T init ) ;
(1) (C++17以降)
(C++20からconstexpr)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2, class T >
ForwardIt2 exclusive_scan ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last,

ForwardIt2 d_first, T init ) ;
(2) (C++17以降)
template < class InputIt, class OutputIt,

class T, class BinaryOp >
OutputIt exclusive_scan ( InputIt first, InputIt last,

OutputIt d_first, T init, BinaryOp op ) ;
(3) (C++17以降)
(C++20からconstexpr)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2,
class T, class BinaryOp >
ForwardIt2 exclusive_scan ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last,

ForwardIt2 d_first, T init, BinaryOp op ) ;
(4) (C++17以降)
1) 次と等価: exclusive_scan ( first, last, d_first, init, std:: plus <> ( )
3) op を使用して排他的プレフィックス和を計算します。
各整数 i について [ 0 , std:: distance ( first, last ) ) の範囲で、以下の操作を順次実行します:
  1. init に続いて [ first , iter ) の要素を順に並べたシーケンスを作成します。ここで iter first の次に来る i 番目 のイテレータです。
  2. シーケンスの op に関する一般化された非可換和を計算します。
  3. 結果を * dest に代入します。ここで dest d_first の次に来る i 番目 のイテレータです。
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 を実際の二項演算として:

  • 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 ] の任意のイテレータまたは部分範囲を無効化する。

目次

翻訳内容: - 「Contents」→「目次」 - その他のC++関連用語(Parameters、Return value、Complexity、Exceptions、Example、See also)は原文のまま保持 - HTMLタグ、属性、数値はすべて変更なし - フォーマットと構造は完全に保持

パラメータ

first, last - 合計する要素の範囲を定義するイテレータのペア
d_first - 出力先範囲の先頭; first と等しくてもよい
policy - 使用する実行ポリシー
init - 初期値
op - 入力イテレータのデリファレンス結果、他の op の結果、および init に適用される二項関数オブジェクト
型要件
-
InputIt LegacyInputIterator の要件を満たさなければならない
-
OutputIt LegacyOutputIterator の要件を満たさなければならない
-
ForwardIt1, ForwardIt2 LegacyForwardIterator の要件を満たさなければならない

戻り値

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

計算量

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

1,2) O(N) 回の std:: plus <> ( ) 適用。
3,4) O(N) 回の op の適用。

例外

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 << "排他的和: ";
    std::exclusive_scan(data.begin(), data.end(),
                        std::ostream_iterator<int>(std::cout, " "),
                        0);
    std::cout << "\n包括的和: ";
    std::inclusive_scan(data.begin(), data.end(),
                        std::ostream_iterator<int>(std::cout, " "));
    std::cout << "\n\n排他的積: ";
    std::exclusive_scan(data.begin(), data.end(),
                        std::ostream_iterator<int>(std::cout, " "),
                        1, std::multiplies<>{});
    std::cout << "\n包括的積: ";
    std::inclusive_scan(data.begin(), data.end(),
                        std::ostream_iterator<int>(std::cout, " "),
                        std::multiplies<>{});
}

出力:

排他的和: 0 3 4 8 9 14 23 25
包括的和: 3 4 8 9 14 23 25 31
排他的積: 1 3 3 12 12 60 540 1080
包括的積: 3 3 12 12 60 540 1080 6480

関連項目

範囲内の隣接する要素間の差を計算する
(関数テンプレート)
範囲の要素を合計または畳み込む
(関数テンプレート)
範囲の要素の部分和を計算する
(関数テンプレート)
呼び出し可能オブジェクトを適用した後、排他的スキャンを計算する
(関数テンプレート)
std::partial_sum と類似し、 i th 番目の入力要素を i th 番目の合計に含める
(関数テンプレート)