株式会社エス・スリー・フォー

STL Samples : <numeric>

数値計算

Faccumulate

- template <class InputIterator, class T>
  T
  accumulate(InputIterator first,
             InputIterator last,
             T init);

- template <class InputIterator, class T, class BinaryOperation>
  T accumulate(InputIterator first,
               InputIterator last,
               T init,
               BinaryOperation binary_op);

initを初期値としてシーケンス[first,last]の各要素を積算します。すなわち:

 T acc = init;
 while ( first != last )
   acc = acc + *first++;
 return acc;

第4引数(BinaryOperation)を渡す形式では:

 T acc = init;
 while ( first != last )
   acc = op(acc,*first++);
 return acc;

と等価です。

Fadjacent_difference

- template <class InputIterator, class OutputIterator>
  OutputIterator
  adjacent_difference(InputIterator first,
                      InputIterator last,
                      OutputIterator result);

- template <class InputIterator, class OutputIterator,
            class BinaryOperation>
  OutputIterator
  adjacent_difference(InputIterator first,
                      InputIterator last,
                      OutputIterator result,
                      BinaryOperation binary_op);

シーケンス[first,last)の隣接する要素の差を[result,…)に作ります。この結果,

 *(result)   = *(first)
 *(result+1) = *(first+1) - *(first)
 *(result+2) = *(first+2) - *(first+1)
 ...

となります。

Finner_product

- template <class InputIterator1, class InputIterator2, class T>
  T
  inner_product(InputIterator1 first1,
                InputIterator1 last1,
                InputIterator2 first2,
                T init);

- template <class InputIterator1, class InputIterator2, class T,
            class BinaryOperation1, class BinaryOperation2>
  T
  inner_product(InputIterator1 first1,
                InputIterator1 last1,
                InputIterator2 first2,
                T init,
                BinaryOperation1 binary_op1,
                BinaryOperation2 binary_op2);

initを初期値とし、シーケンス[first1,last1)と[first2,…)との内積を求めます。すなわち:

 T acc = init;
 while ( first1 != last1 )
   acc = acc + (*first1++) * (*first2++)
 return acc;

第4,5引数(BinaryOperation)を渡す形式では:

 T acc = init;
 while ( first1 != last1 )
   acc = op1(acc, op2(*first1++, *first2++));
 return acc;

と等価です。

Fpartial_sum

- template <class InputIterator, class OutputIterator>
  OutputIterator
  partial_sum(InputIterator first,
              InputIterator last,
              OutputIterator result);

- template <class InputIterator, class OutputIterator,
            class BinaryOperation>
  OutputIterator
  partial_sum(InputIterator first,
              InputIterator last,
              OutputIterator result,
              BinaryOperation binary_op);

0 < n <= (last-first)なるnに対し、シーケンス[first,first+n)の積算値を*(result+n)に作ります。この結果,

 *(result)   = *(first)
 *(result+1) = *(first) + *(first+1)
 *(result+2) = *(first) + *(first+1) + *(first+2)
 ...

となります。

SOURCE

#include <iostream>
#include <numeric>
#include "to_string.h"

using namespace std;

void std_accumulate() {
  cout << "accumulate" << endl;
  const int N = 5;
  int input[N] = { 1,2,3,4,5 };
  cout << to_string(input,input+N) << " の総和: "
       << accumulate(input, input+N, 0) << endl;
}

void std_adjacent_difference() {
  cout << "adjacent_difference" << endl;
  const int N = 5;
  int input[N]  = { 2,3,4,5,6 };
  int output[] = { 0,0,0,0,0 };
  cout << to_string(input,input+5) << " の隣接要素の差: ";
  adjacent_difference(input, input+N, output);
  cout << to_string(output, output+N) << endl;
}

void std_inner_product() {
  cout << "innser_product" << endl;
  const int N = 5;
  int input1[N] = { 1,2,3,4,5 };
  int input2[N] = { 1,2,3,4,5 };
  cout << to_string(input1,input1+N) << " と "
       << to_string(input2,input2+N) << " の内積: "
       << inner_product(input1, input1+N, input2, 0) << endl;
}

void std_partial_sum() {
  cout << "partial_sum" << endl;
  const int N = 5;
  int input[N]  = { 1,2,3,4,5 };
  int output[N] = { 0,0,0,0,0 };
  cout << to_string(input,input+N) << " の部分和: ";
  partial_sum(input, input+N, output);
  cout << to_string(output, output+N) << endl;
}