# Calculate Dot Product of Two Vectors in C++

This article will demonstrate multiple methods to calculate the dot product of two vectors in C++.

The dot product is the sum of the products of the corresponding elements of the two vectors. Suppose we have two vectors - `{1, 2, 3}` and `{4, 5, 6}`, and the dot product of these vectors is `1*4 + 2*5 + 3*6 = 32`.

## Use `std::inner_product` to Calculate Dot Product of Two Vectors in C++

`std::inner_product` is part of the C++ numeric algorithms library included in the `<numeric>` header. The method calculates the sum of products on two ranges, the first of which is specified with `begin`/`end` iterators and the second range with only `begin`. The function also takes `init` as the fourth parameter to initialize the accumulator value. The return value is the final dot product value of the given ranges. Note that `std::inner_product` always performs operations in a given order.

``````#include <iostream>
#include <vector>
#include <numeric>
#include <iterator>

using std::cout;
using std::endl;
using std::vector;
using std::copy;

int main(){
vector<int> vec1 { 1, 2, 3, 4, 5 };
vector<int> vec2 { 2, 4, 6, 8, 10 };

copy(vec1.begin(), vec1.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;

copy(vec2.begin(), vec2.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;

cout << "Scalar product is: "
<< inner_product(vec1.begin(), vec1.end(), vec2.begin(), 0);
cout << endl;

return EXIT_SUCCESS;
}
``````

Output:

``````1; 2; 3; 4; 5;
2; 4; 6; 8; 10;
Scalar product is: 110
``````

## Use `std::transform_reduce` to Calculate Dot Product of Two Vectors in C++

Unlike the previous method, `std::transform_reduce` can perform operations on ranges out of order, thus maximizing performance. `std::transform_reduce` is essentially the parallelized version of the `std::inner_product` algorithm. The following example demonstrates the function’s execution with the same arguments as passed in the previous example.

``````#include <iostream>
#include <vector>
#include <numeric>
#include <iterator>

using std::cout;
using std::endl;
using std::vector;
using std::copy;
using std::inner_product;

int main(){
vector<int> vec1 { 1, 2, 3, 4, 5 };
vector<int> vec2 { 2, 4, 6, 8, 10 };

copy(vec1.begin(), vec1.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;

copy(vec2.begin(), vec2.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;

cout << "Scalar product is: "
<< std::transform_reduce(vec1.begin(),
vec1.end(), vec2.begin(), 0);

return EXIT_SUCCESS;
}
``````

Output:

``````1; 2; 3; 4; 5;
2; 4; 6; 8; 10;
Scalar product is: 110
``````

## Use `std::transform_reduce` and `std::execution::par` Calculate Dot Product of Two Vectors

Alternatively, one can additionally specify an execution policy for the `std::transform_reduce` algorithm. This method offers more control to the programmer as the program flow can be customized with rules of execution as defined in this manual. Even though there are performance gains in using `transform_reduce`, one should always mind potential race conditions when specifying parallel execution policy.

``````#include <iostream>
#include <vector>
#include <numeric>
#include <iterator>
#include <execution>

using std::cout;
using std::endl;
using std::vector;
using std::copy;
using std::inner_product;

int main(){
vector<int> vec1 { 1, 2, 3, 4, 5 };
vector<int> vec2 { 2, 4, 6, 8, 10 };

copy(vec1.begin(), vec1.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;

copy(vec2.begin(), vec2.end(),
std::ostream_iterator<int>(cout,"; "));
cout << endl;

cout << "Scalar product is: "
<< std::transform_reduce(std::execution::par,
vec1.begin(), vec1.end(),
vec2.begin(), 0);

return EXIT_SUCCESS;
}
``````

Output:

``````1; 2; 3; 4; 5;
2; 4; 6; 8; 10;
Scalar product is: 110
``````

## Related Article - C++ Vector

• Shuffle Vector in C++
• Check if Element Exists in C++ Vector