如何折叠STL容器? [英] How to fold STL container?

查看:129
本文介绍了如何折叠STL容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要Haskell的与foldl 功能的模拟折叠的STL容器。预期的签名是类似以下内容:

I need an analog of Haskell's foldl function to fold any STL containers. Expected signature is like following:

template Iterator, FoldingFunction, Result
Result foldl(
  Iterator begin, 
  Iterator end, 
  FoldingFunction f, 
  Result initValue);

标准STL没有这样的功能。是否加速的有什么?

我知道这是pretty实现简单,但我想知道是否有任何现成的标准化的实现。

I know it's pretty simple to implement, but I'd like to know whether there's any ready standardized implementation.

还有一个问题:你怎么通常折叠数据列出了C ++ / STL

And one more question: how do you usually fold data lists in C++/STL?

推荐答案

STL确实有这样的功能:的 的std ::积累 。然而,在头<数字> ,而不是<算法方式>

STL does have such a function: std::accumulate. However, it is in the header <numeric>, not <algorithm>.

事实上的折叠的维基百科页面已经列出的在大多数编程语言,包括C ++与foldl / foldr相似功能。

Actually the Wikipedia page on "Fold" already listed the foldl/foldr functions on most programming languages, including C++.

这篇关于如何折叠STL容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆