Aug 12, 2009

Be Careful with stl::accumulate

If we are to accumulate a vector of doubles, use the following code snippet:

accumulate(timbre_topic_dist.begin(), timbre_topic_dist.end(), 0.0);

You do not want to be lazy and write 0.0 as 0, which will be interpreted by the compiler as an integer, which is used to infer the type of intermediate and final result of accumulate. For your reference, here attaches one of the multiple prototypes of accumulate:
      template < typename _InputIterator, typename _Tp >
_Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init) {
for (; __first != __last; ++__first)
__init = __init + *__first;
return __init;
Note that the partial result is stored in _Tp __init, which means even we explicitly use plus as the accumulator, the result will still be truncated.

accumulate(timbre_topic_dist.begin(), timbre_topic_dist.end(), 0, // Wrong
plus()); // No effect to correct the mistake.

No comments: