Not exactly the question you asked, but there is a simple error in your code example. The initial value in accumulate is a pattern, and in your code, integers are its pattern. If you give him a set of doubles, they will be cast from integers, and you will receive incorrect answers. Having made this mistake before, I made myself a quick warranty character as follows:
template<class InputIterator, class T> inline T accumulate_checked(InputIterator first, InputIterator last, T init ) { return std::accumulate(first,last, init); }
I thought I would share it if it would be interesting.
Just for completeness, your function might look like this:
double mean_array( double *array, size_t count ) { double sum = std::accumulate(array,array+count,0.0) return sum / count; }
or be especially careful
double mean_array( double *array, size_t count ) { double sum = accumulate_checked(array,array+count,0.0) return sum / count; }
or even better didier trosset template version
source share