名前空間
変種
操作

std::inclusive_scan

From cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムとRangeアルゴリズム (C++20)
制約付きアルゴリズム、例: ranges::copy, ranges::sort, ...
実行ポリシー (C++17)
シーケンスを変更しない操作
一括操作
(C++17)
検索操作
(C++11)                (C++11)(C++11)

シーケンスを変更する操作
コピー操作
(C++11)
(C++11)
スワップ操作
変換操作
生成操作
削除操作
順序変更操作
(C++17まで)(C++11)
(C++20)(C++20)
サンプリング操作
(C++17)

ソートおよび関連操作
パーティション操作
ソート操作
二分探索操作
(パーティション化された範囲)
集合操作 (ソート済み範囲)
マージ操作 (ソート済み範囲)
ヒープ操作
最小/最大操作
(C++11)
(C++17)
辞書順比較操作
順列操作
Cライブラリ
数値演算
inclusive_scan
(C++17)   
未初期化メモリに対する操作
 
 
ヘッダー <numeric> で定義
template< class InputIt, class OutputIt >

OutputIt inclusive_scan( InputIt first, InputIt last,

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

          class ForwardIt1, class ForwardIt2 >
ForwardIt2 inclusive_scan( ExecutionPolicy&& policy,
                           ForwardIt1 first, ForwardIt1 last,

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

OutputIt inclusive_scan( InputIt first, InputIt last,

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

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

                           ForwardIt2 d_first, BinaryOp op );
(4) (C++17以降)
template< class InputIt, class OutputIt,

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

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

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

                           ForwardIt2 d_first, BinaryOp op, T init );
(6) (C++17以降)
1) inclusive_scan(first, last, d_first, std::plus<>() と等価です。
3) op を使用してインクルーシブプレフィックス合計を計算します。
[0std::distance(first, last)) の各整数 i に対して、次の操作を順に実行します。
  1. firsti 番目のイテレータである iter まで、[firstiter] の要素で構成されるシーケンスを順に作成します。
  2. op を使ってシーケンスの一般化された非可換和を計算します。
  3. d_firsti 番目のイテレータである dest に結果を代入します。
5) (3) と同じですが、作成される各シーケンスは init の後に [firstiter] の要素が順に続く形で構成されます。
2,4,6) (1,3,5) と同じですが、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つの隣接する要素 elem1elem2 を選択します。
  2. binary_op(elem1, elem2) を計算し、シーケンス内の2つの要素を結果で置き換えます。
  3. シーケンスに1つの要素だけが残るまでステップ1と2を繰り返します。


実際のバイナリ操作として binary_op が与えられた場合

  • binary_op が結合法則に従わない場合 (浮動小数点数の加算など)、結果は非決定論的です。
  • オーバーロード (1-4) の場合、binary_op(*first, *first)decltype(first)値型に変換できない場合、プログラムは不正です。
  • オーバーロード (5,6) の場合、次の値のいずれかが T に変換できない場合、プログラムは不正です。
  • binary_op(init, *first)
  • binary_op(init, init)
  • binary_op(*first, *first)
  • 次のいずれかの条件が満たされる場合、動作は未定義です。
  • オーバーロード (1-4) の場合、decltype(first) の値型がMoveConstructible ではない。
  • オーバーロード (5,6) の場合、TMoveConstructible ではない。
  • binary_op[firstlast) のいずれかの要素を変更する。
  • binary_op[firstlast] の任意のイテレータまたはサブレンジを無効にする。

目次

[編集] パラメーター

first, last - 合計する要素のソース範囲を定義するイテレータのペア
d_first - デスティネーション範囲の先頭。first と同じであってもよい。
policy - 使用する 実行ポリシー
init - 初期値
op - 入力イテレータのデリファレンス結果、他の op の結果、および init (指定されている場合) に適用されるバイナリFunctionObject
型要件
-
InputItLegacyInputIterator の要件を満たす必要があります。
-
OutputItLegacyOutputIterator の要件を満たさなければなりません。
-
ForwardIt1, ForwardIt2LegacyForwardIterator の要件を満たさなければなりません。

[編集] 戻り値

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

[編集] 計算量

std::distance(first, last)N とする

1,2) O(N) 回の std::plus<>() の適用。
3-6) 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 << "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

[編集] 関連項目

範囲内の隣接する要素間の差を計算する
(関数テンプレート) [編集]
範囲の要素を合計または畳み込む
(関数テンプレート) [編集]
範囲の要素の部分和を計算する
(関数テンプレート) [編集]
呼び出し可能オブジェクトを適用し、インクルーシブスキャンを計算する
(関数テンプレート) [編集]
std::partial_sum に似ているが、i 番目の入力要素を i 番目の合計から除外する
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)