inner_product()

inner_product() accumulates the product of two sequences of values, adding them in turn to a user-specified initial value. For example, given the two sequences {2,3,5,8} and {1,2,3,4}, the result is the sum of the product pairs (2*1)+(3*2)+(5*3)+(8*4). If we provide an initial value of 0, the result is 55.

A second version allows us to override the default addition operation and the default multiply operation. For example, if we use the same sequence but specify subtraction and addition, the result is the difference of the following addition pairs: (2+1)–(3+2)–(5+3)–(8+4). If we provide an initial value of 0, the result is -28.

 #include <numeric> int ia[] = { 2, 3, 5, 8 }; int ia2[] = { 1, 2, 3, 4 }; int res = inner_product( ...

Get Essential C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.