私はstd::istream
から取得した番号の束の合計を計算したい場合は、私は次の操作を行うことができますが:ラン同じ入力イテレータ範囲に並んで2 <algorithm>の側
// std::istream & is = ...
int total = std::accumulate(std::istream_iterator<int>(is),
std::istream_iterator<int>(),
0);
しかし、私の場合その平均値を計算したい、私は2つの異なる結果を蓄積する必要があります。
- 合計(
std::accumulate
) - 総数(
std::distance
)
これら2つのアルゴリズムを「マージ」し、イテレータ範囲の1回のパスで「並列」する方法はありますか?私は次のようなことをしたい:
using std::placeholders;
int total, count;
std::tie(total, count) = merge_somehow(std::istream_iterator<int>(is),
std::istream_iterator<int>(),
std::bind(std::accumulate, _1, _2, 0),
std::distance);
double average = (double)total/count;
これは可能ですか?