Dynamic structures

I have a question.Does anybody know how to find the average value of all the elements in a deque?
I would iterate through the std::deque, add the values into a variable, and then divide it by the amount of elements.
Same as for any other container:
std::cout << std::accumulate(d.begin(), d.end(), 0.0) / d.size();

For extra flair, you could run it through boost accumulators (although it's only beneficial when you're accumulating multiple statistics a single pass):
accumulator_set<double, stats<tag::mean>> acc;
acc = for_each(d.begin(), d.end(), acc);
std::cout << mean(acc);
Last edited on
Topic archived. No new replies allowed.