回答編集履歴

1 修正

episteme

episteme score 9923

2018/02/16 09:29  投稿

「合計、平均、標準偏差を算出」んとこだけ。
```C++
#include <numeric>
#include <iterator>
#include <cmath>
#include <tuple>
template<typename InputIterator>
std::tuple<double,double,double>
statistics(InputIterator first, InputIterator last) {
 size_t n = std::distance(first, last);
 double sum = std::accumulate(first, last, 0.0);
statistics(InputIterator first, size_t n) {
 double sum = std::accumulate(first, std::next(first,n), 0.0);
 double mean = sum / n;
 double stddev = sqrt(std::inner_product(first, last, first, 0.0)/n - mean*mean);
 double stddev = sqrt(std::inner_product(first, std::next(first,n), first, 0.0)/n - mean*mean);
 return { sum, mean, stddev };
}
/*
* おためし
*/
#include <iostream>
int main() {
 using namespace std;
 auto input = { 0.01, 0.02, 0.03, 0.04, 0.05 };
 double input[] = { 0.01, 0.02, 0.03, 0.04, 0.05 };
 double sum, mean, stddev;
 tie(sum, mean,stddev) = statistics(begin(input), end(input));
 tie(sum, mean, stddev) = statistics(input, 5);
 cout << "sum = " << sum << "\n"
         "mean = " << mean << "\n"
         "standard deviation = " << stddev
      << endl;
}
/* 実行結果
sum = 0.15
mean = 0.03
standard deviation = 0.0141421
*/
```

思考するエンジニアのためのQ&Aサイト「teratail」について詳しく知る