lobot::accumulator< T > Class Template Reference

#include <Robots/LoBot/util/LoSTL.H>

Collaboration diagram for lobot::accumulator< T >:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 accumulator (T init=T())
accumulatoroperator= (T t)
accumulatoroperator* ()
accumulatoroperator++ ()
accumulator operator++ (int)
value () const

Detailed Description

template<typename T>
class lobot::accumulator< T >

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.


The documentation for this class was generated from the following file:
Generated on Sun May 8 08:44:39 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3