C++ std::inner_product() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
C++98

Definition

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);
CallWhat 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

ParameterDescription
first1, last1Required. The first range [first1, last1). Its length sets how many pairs are used.
first2Required. The start of the second range. It must hold at least as many elements as the first range.
initRequired. The start value. Its type is the type of the result.
op1Optional. Combines the running value with a pair result. The default is +.
op2Optional. Works on one pair of elements. The default is *.

Return Value

Return ValueDescription
Value of type TThe 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;
}

How It Works

  1. The first range is all of a. The second range starts at b.begin() and is as long as a.
  2. Pairs are multiplied: 2*5 = 10, 3*6 = 18, and 4*7 = 28.
  3. 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.

C++
#include <cstdlib>
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>

int main() {
    std::vector<int> a = {1, 5, 9};
    std::vector<int> b = {2, 3, 12};

    int distance = std::inner_product(
        a.begin(), a.end(), b.begin(), 0,
        std::plus<int>(),
        [](int x, int y) { return std::abs(x - y); });

    std::cout << "total distance = " << distance << '\n';
    return 0;
}

How It Works

  1. This time op1 is std::plus<int>(), which adds the pair results, and op2 is a lambda.
  2. The lambda is called for each pair and returns the gap between the two numbers: |1-2| = 1, |5-3| = 2, and |9-12| = 3.
  3. plus adds the gaps to 0, so the total distance is 1 + 2 + 3 = 6.

Example 3 — Weighted Average of Scores

Multiply every score by its weight, add the results, then divide by the sum of the weights.

C++
#include <iostream>
#include <numeric>
#include <vector>

int main() {
    std::vector<int> scores  = {80, 90, 70};
    std::vector<int> weights = {2, 3, 5};

    double total = std::inner_product(scores.begin(), scores.end(),
                                      weights.begin(), 0.0);
    int weightSum = std::accumulate(weights.begin(), weights.end(), 0);

    std::cout << "weighted total: " << total << '\n';
    std::cout << "weighted average: " << total / weightSum << '\n';
    return 0;
}

How It Works

  1. Each score has a weight at the same position. The weights show that the third score counts the most.
  2. 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.
  3. 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.

Next: iota()

Fill a range with a growing sequence: start, start+1, start+2, and so on with <numeric>.

iota() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful