#include <Robots/LoBot/util/LoSTL.H>
Public Member Functions | |
accumulator (T init=T()) | |
accumulator & | operator= (T t) |
accumulator & | operator* () |
accumulator & | operator++ () |
accumulator | operator++ (int) |
T | value () const |
This object acts as an iterator that can be passed to algorithms such as std::transform. However, instead of iterating across a container, this iterator accumulates the source values passed to it. Typical usage of this is shown below:
accumulator<int> acc = std::transform(begin, end, accumulator<int>(0), f) ; std::cout << "accumulated value is: " << acc.value() << '
' ;
In the above example, f is a function or object that takes arguments of the type contained between begin and end and returns an int.
Naturally, the std::accumulate algorithm would do the exact same thing as the above algorithm with a lot less fuss. However, accumulate cannot work with two different sequences. For instance, if we have two sequences and we would like to determine a least squares error between the two sequences using an STL algorithm and without having to write a custom function or function object to take care of the whole thing, this accumulator type along with boost::bind, std::minus and a squaring function is just the ticket:
accumulator<float> acc = std::transform(begin1, end1, begin2, accumulator<float>(0), boost::bind(sqr<float>, boost::bind(std::minus<float>, _1, _2))); std::cout << "square error is: " << acc.value() << '
' ;
NOTE: The sqr used above is defined in util/LoMath.H.
Definition at line 197 of file LoSTL.H.