Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fold STL container?

Tags:

c++

stl

boost

fold

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); 

Standard STL has no such function. Does Boost have any?

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

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

like image 206
Andrey Avatar asked Oct 11 '10 13:10

Andrey


1 Answers

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

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

like image 118
kennytm Avatar answered Sep 20 '22 15:09

kennytm