C++ C+中更优雅的boost累加+;11?

C++ C+中更优雅的boost累加+;11?,c++,c++11,boost,C++,C++11,Boost,以下示例说明如何使用boost::accumulate: // The data for which we wish to calculate statistical properties: std::vector< double > data( /* stuff */ ); // The accumulator set which will calculate the properties for us: accumulator_set< double, features&

以下示例说明如何使用
boost::accumulate

// The data for which we wish to calculate statistical properties:
std::vector< double > data( /* stuff */ );

// The accumulator set which will calculate the properties for us:
accumulator_set< double, features< tag::tail<left> > > acc(
    tag::tail<left>::cache_size = 4 );

// Use std::for_each to accumulate the statistical properties:
std::for_each( data.begin(), data.end(), bind<void>( ref(acc), _1 ) );
//我们希望计算统计特性的数据:
std::vector数据(/*stuff*/);
//将为我们计算属性的累加器集:
累加器组>acc(
tag::tail::cache_size=4);
//使用std::for_分别累积统计特性:
std::for_each(data.begin()、data.end()、bind(ref(acc),_1));

在C++11/14中,有没有更优雅的方法用基于范围的循环或lambda编写此代码?

我可以想到两种方法,如下所示:

std::vector< double > data = {2.1, 2.2, 3.3, 4.4, 5.5};
accumulator_set< double, features< tag::tail<left> > > acc(tag::tail<left>::cache_size = 4);

for_each(data.begin(), data.end(), [&acc](double y){ acc(y); });
std::vectordata={2.1,2.2,3.3,4.4,5};
累加器集>acc(tag::tail::cache\u size=4);
对于每个(data.begin()、data.end()、[&acc](双y){acc(y);});

std::vectordata={2.1,2.2,3.3,4.4,5};
累加器集>acc(tag::tail::cache\u size=4);
用于(自动y:数据)
行政协调会(y);
std::vector data={2.1,2.2,3.3,4.4,5.5};
std::设置最小值;
用于(自动和x:数据){
至少插入(decltype(x)(x));
如果(最小.size()>4)
least.erase(std::prev(least.end());
}

就字符而言,大小基本相同。因为它使用了众所周知的
std
原语,所以更容易阅读和理解。

这段代码应该做什么?跟踪最小的4个值。在我的问题中描述的是关于累加器框架的更一般性的尾部,对于每一行来说,看到差异是非常有趣的。我认为它会在不同的运行时成本下做不同的事情。这会处理tail,但正如我在评论中提到的,我对累加器框架更感兴趣。代码片段只是从文档中提取的一个特定示例
std::vector< double > data = {2.1, 2.2, 3.3, 4.4, 5.5};
accumulator_set< double, features< tag::tail<left> > > acc(tag::tail<left>::cache_size = 4);

for (auto y : data)
    acc(y);
std::vector<double> data = {2.1, 2.2, 3.3, 4.4, 5.5};
std::set<double> least;
for (auto&& x:data) {
  least.insert(decltype(x)(x));
  if (least.size() > 4)
    least.erase( std::prev(least.end()) );
}