In C++, std::inner_product() takes two ranges and walks through them side by side. By default it multiplies each pair of matching elements and adds the products to a start value. In maths this is called the dot product.
It is handy for weighted totals, such as a final grade from scores and weights. You can replace the multiply and the add with your own operations to do other pair-by-pair jobs. The second range is given only by its start iterator.
This function is declared in the <numeric> header. It exists since C++98, so every compiler supports it. It is a free function, called as std::inner_product(...), not with a dot.
Syntax
Give the first range, the start of the second range, and an initial value. Two custom operations are optional.
C++
T a = std::inner_product(first1, last1, first2, init);
T b = std::inner_product(first1, last1, first2, init, op1, op2);
int dot = std::inner_product(a.begin(), a.end(), b.begin(), 0);
Call
What it does
inner_product(f1, l1, f2, init)
Computes init + a1*b1 + a2*b2 + ... for the two ranges.
inner_product(f1, l1, f2, init, op1, op2)
Computes init op1 (a1 op2 b1) op1 (a2 op2 b2) .... The op2 works on each pair, and op1 combines the results.
In the long form, op2 works on each pair first, then op1 folds the results from left to right.
Library declarations
C++
template<class InputIt1, class InputIt2, class T>
T inner_product(InputIt1 first1, InputIt1 last1,
InputIt2 first2, T init);
template<class InputIt1, class InputIt2, class T,
class BinaryOp1, class BinaryOp2>
T inner_product(InputIt1 first1, InputIt1 last1,
InputIt2 first2, T init,
BinaryOp1 op1, BinaryOp2 op2);
Parameters
Parameter
Description
first1, last1
Required. The first range [first1, last1). Its length sets how many pairs are used.
first2
Required. The start of the second range. It must hold at least as many elements as the first range.
init
Required. The start value. Its type is the type of the result.
op1
Optional. Combines the running value with a pair result. The default is +.
op2
Optional. Works on one pair of elements. The default is *.
Return Value
Return Value
Description
Value of type T
The folded result. For an empty range it is init.
C++ Examples
Example 1 — Dot Product of Two Vectors
Multiply matching elements and add the products.
C++
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> a = {2, 3, 4};
std::vector<int> b = {5, 6, 7};
int dot = std::inner_product(a.begin(), a.end(), b.begin(), 0);
std::cout << "dot = " << dot << '\n';
return 0;
}
Output
dot = 56
How It Works
The first range is all of a. The second range starts at b.begin() and is as long as a.
Pairs are multiplied: 2*5 = 10, 3*6 = 18, and 4*7 = 28.
The products are added to init = 0, giving 10 + 18 + 28 = 56.
Example 2 — Custom Operations: Sum of Differences
Replace the multiply with a lambda that measures the distance between two numbers.
Each score has a weight at the same position. The weights show that the third score counts the most.
inner_product() computes 80*2 + 90*3 + 70*5, which is 160 + 270 + 350 = 780. Because init is 0.0, the answer is a double.
std::accumulate() adds the weights to get 10. Dividing 780 by 10 gives the weighted average 78.
Frequently Asked Questions
It takes two ranges, multiplies the elements that sit at the same position, and adds all the products to a start value. This is the dot product of two vectors.
Pass two extra arguments after init: op1 combines the running value with each pair result, and op2 works on each pair, for example a lambda that returns the difference.
It must be at least as long as the first range. inner_product() only gets the start of the second range and does not check its length, so a shorter range causes undefined behaviour.
For a dot product they give the same answer. transform_reduce() is C++17 and can run in parallel. inner_product() is older and always works left to right.
The result type is the type of init. If init is the int 0, the sum stays an int. Pass 0.0 as init when you want a double result.