c – 在相同的输入迭代器范围上并行运行两个
发布时间:2020-12-16 05:32:04 所属栏目:百科 来源:网络整理
导读:如果我想计算从std :: istream检索的一堆数字的总和,我可以执行以下操作: // std::istream is = ...int total = std::accumulate(std::istream_iteratorint(is),std::istream_iteratorint(),0); 但是,如果我想计算他们的平均值,我需要积累两个不同的结果:
如果我想计算从std :: istream检索的一堆数字的总和,我可以执行以下操作:
// std::istream & is = ... int total = std::accumulate(std::istream_iterator<int>(is),std::istream_iterator<int>(),0); 但是,如果我想计算他们的平均值,我需要积累两个不同的结果: >总和(std :: accumulate) 有没有办法“合并”这两种算法,并且在迭代器范围的单次传递中并行运行它们?我想做一些像: using std::placeholders; int total,count; std::tie(total,count) = merge_somehow(std::istream_iterator<int>(is),std::bind(std::accumulate,_1,_2,0),std::distance); double average = (double)total / count; 这可能吗? 解决方法
这种单程积分的现成解决方案是由
Boost.Accumulators实现的.您可以使用一个累加器来表示总和,计数和平均值,然后填充它,然后在最后提取所有三个结果.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |