Dot product of two vectors in C++

Key takeaways:

  • The dot product of two vectors results in a scalar and is used to understand the relationship between vectors, such as their angle or magnitude.

  • To calculate the dot product, multiply the corresponding components of the vectors and add the products together. For example, use the following formula to calculate the dot product of two vectors: k=x1y1+x2y2+x3y3k=x1​∗y1​+x2​∗y2​+x3∗​y3.

  • The dot product can also be expressed as k=v1v2cosθk=∣v1​∣∣v2​∣cosθ, where θθ is the angle between the vectors.

  • In C++, the dot product can be computed using the inner_product() function or an iterative approach that multiplies and sums the corresponding components of the vectors.

  • The dot product is used in fields like physics, computer graphics, and machine learning for tasks such as vector projections, lighting calculations, and similarity measurements.

What is the dot product?

The dot product, also known as the inner product or scalar product, is a mathematical operation in which two vectors result in a scalar (a single numeric value). The dot product is defined for vectors in both 2D and 3D to understand and deal with vector behaviors. It is used in various fields, including physics, mathematics, and computer graphics.

Let's discuss the formulas to calculate the dot product of two vectors.

Algebraic formula to calculate dot product of two vectors

To calculate the dot product between two vectors, we multiply their corresponding values along the x-, y-, and z- axes before adding them. Suppose we have two vectors: v1v1 and v2v2 such that:

v1=(a1,a2,a3)v1 = (a1, a2,a3)

v2=(b1,b2,b3)v2 = (b1,b2,b3)

So the dot product is calculated as:

Here, kk is the resultant scalar of the dot product.

Geometric formula

We use the following formula to calculate the dot product for the vectors geometrically when we know the angle between them.

  • kk : The resultant scalar of the dot product.

  • v1|v1| : The magnitude of the vector v1v1.

  • v2|v2| : The magnitude of the vector v2v2.

  • θθ : The angle between vectors v1v1 and v2v2.

Calculation of the dot product in C++

Let's calculate the dot product of two vectors in C++ now. The following examples refer to the algebraic formula while calculating the dot product.

1. Using the inner_product function

We can calculate the dot product of two vectors using the inner_product function from the <numeric> header file, which is part of the Standard Template Library (STL). To include the <numeric> header file, we write the following code:

#include <numeric>
Including numeric library

Now let's discuss the syntax for using inner_product function:

template <class InputIt1, class InputIt2, class T>
T inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init);
Syntax of inner_product() function
  • class InputIt1, class InputIt2, class T: The template parameters that represent the placeholders for the types of arguments that the function template can accept.

  • first1: The iterator that points to the beginning of the first vector.

  • last1: The iterator that points to the end of the first vector.

  • first2: The iterator that points to the beginning of the second vector.

  • init: The initial value for the accumulation of the dot product. We start adding the dot product from this value.

Implementing the dot product in C++

The following code demonstrates how to calculate the dot product of two vectors using the inner_product function.

#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main() {
vector<int> v1 = {2, 3, 1};
vector<int> v2 = {4, 2, 5};
int dotProduct = inner_product(v1.begin(), v1.end(), v2.begin(), 0);
cout << "Dot product: " << dotProduct << endl;
return 0;
}
  • Lines 1–3: We include the necessary libraries.

  • Line 4: We bring all the names from the std namespace into the code so we can avoid prefixing them with std::.

  • Lines 7–8: We define the two vectors whose dot product we want to calculate of.

  • Line 10: We calculate the dot product of the two vectors using inner_product function. We send iterators (v1.begin() and v2.begin()) that point to the beginning of each vector, and v1.end() that points just past the last element of v1 as parameters. The last argument 0 is the initial value for the accumulation of the dot product.

  • Line 12: We print the dot product.

Ready to master C++?

Our Learn C++ course will help you build practical skills, from basic loops to complex program structures. Join now and start coding your way to success!

2. Using the iterative approach

We use an iterative approach to calculate the dot product of the vectors. We use a loop to iterate through the axes values of both vectors, multiply the corresponding values, and add the results together.

Implementing the dot product using the iterative approach

The following code demonstrates how to calculate the dot product of two vectors using an iteration.

#include <iostream>
#include <vector>
using namespace std;
int dotProduct( vector<int> v1, vector<int> v2) {
int result = 0;
for (int i = 0; i < v1.size(); ++i) {
result += v1[i] * v2[i];
}
return result;
}
int main() {
// In case of any missing axes write 0
vector<int> v1 = {2, 3, 1};
vector<int> v2 = {4, 2, 5};
int dotProductResult = dotProduct(v1, v2);
cout << "Dot product: " << dotProductResult << endl;
return 0;
}
  • Lines 5–11: In the function dotProduct, we multiply the corresponding values of each axis of the vectors and add them in a variable result.

  • Lines 15–16: We define the two vectors we want to calculate the dot product of.

  • Line 18: We make a call to the function dotProduct.

Conclusion

By understanding the concept and implementation of the dot product in C++, we can perform various mathematical computations efficiently and use the dot product in a wide range of applications, including vector manipulations, physics simulations, and computer graphics rendering.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is the dot product of IJK?

The dot product of the unit vectors i,j,ki,j,k is 0 when calculated between different unit vectors, because they are orthogonal to each other.


Is work scalar or vector?

Work is a scalar quantity.


Is the dot product a scalar?

Yes, the dot product results in a scalar.


What is the dot product of 2i 4j 5k and 3i 2j k?

The dot product is:

=2×3+4×2+5×1= 2 × 3 + 4 × 2 + 5 × 1 =6+8+5= 6 + 8 + 5 =19= 19.


Is electric current a vector?

No, electric current is a scalar quantity.


How to check if a vector is empty in C++

Use the vector.empty() function. It returns true if the vector is empty.


What is the difference between the dot product and the cross product?

The dot product results in a scalar and measures similarity, while the cross product results in a vector and measures perpendicularity.


Unlock your potential: Vector operations series, all in one place!

Continue your exploration of vector operations by checking out our series of Answers below:

Free Resources

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved