Using the accumulate Algorithm

Figure 16.15 uses the accumulate numeric algorithm (introduced in Fig. 16.5) to calculate the sum of the squares of the elements in an array. The fourth argument to accumulate is a binary function object (that is, a function object for which operator() takes two arguments) or a function pointer to a binary function (that is, a function that takes two arguments). Function accumulate is demonstrated twice—once with a function pointer and once with a function object.

 1   // Fig. 16.15: fig16_15.cpp 2   // Demonstrating function objects. 3   #include <iostream> 4   #include <array> // array class-template definition 5   #include <algorithm> // copy algorithm 6   #include <numeric> // ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.