function template
<numeric>
std::inner_product sum/multiply (1)template <class InputIterator1, class InputIterator2, class T> T inner_product (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);custom (2)
template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2> T inner_product (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
Compute cumulative inner product of range
Returns the result of accumulating init with the inner products of the pairs formed by the elements of two ranges starting at first1 and first2.The two default operations (to add up the result of multiplying the pairs) may be overridden by the arguments binary_op1 and binary_op2.
The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
template <class InputIterator1, class InputIterator2, class T>
T inner_product (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, T init)
{
while (first1!=last1) {
init = init + (*first1)*(*first2);
// or: init = binary_op1 (init, binary_op2(*first1,*first2));
++first1; ++first2;
}
return init;
}
[first1,last1)
, which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.
[first1,last1)
).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// inner_product example
#include <iostream> // std::cout
#include <functional> // std::minus, std::divides
#include <numeric> // std::inner_product
int myaccumulator (int x, int y) {return x-y;}
int myproduct (int x, int y) {return x+y;}
int main () {
int init = 100;
int series1[] = {10,20,30};
int series2[] = {1,2,3};
std::cout << "using default inner_product: ";
std::cout << std::inner_product(series1,series1+3,series2,init);
std::cout << '\n';
std::cout << "using functional operations: ";
std::cout << std::inner_product(series1,series1+3,series2,init,
std::minus<int>(),std::divides<int>());
std::cout << '\n';
std::cout << "using custom functions: ";
std::cout << std::inner_product(series1,series1+3,series2,init,
myaccumulator,myproduct);
std::cout << '\n';
return 0;
}
using default inner_product: 240 using functional operations: 70 using custom functions: 34
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4