C++

What is the Accumulate Function in C++?

There are certain built-in functions in the C++ programming language that makes it all easier for us to perform our routine tasks. Especially, while dealing with a wide range of values or data, we require such functions that can automate those tasks that are difficult to handle manually. The accumulate function in C++ is also used to serve the very same purpose and in this guide, we will see how this function works in C++ in Ubuntu 20.04.

The Accumulate Function in C++ in Ubuntu 20.04

The word “accumulate” literally means gathering something together i.e. gradually stock up something to calculate a whole result. The accumulate function in C++ in its simplest form is used to calculate the sum of a wide range of different values. However, it can also be used to serve other purposes such as calculating the product of a wide range of values. The simplest syntax of this function in C++ is as follows:

std::accumulate(starting_range(), ending_range(), initial_value);

The accumulate function in C++ by default accepts three arguments (an optional fourth argument can also be used with it which we will discuss later). The first argument i.e. the starting_range() refers to the first value of the range to be accumulated, the second argument i.e., the ending_range() refers to the last value of the range to be accumulated, whereas the third argument i.e. the initial_value refers to the value assigned to the variable in which the result of the accumulate function is supposed to be stored.

Now, coming towards a relatively complex syntax of the accumulate function in C++ which is as follows:

std::accumulate(starting_range(), ending_range(), initial_value, built-in binary predicate);

In this second syntax, all the values that have been discussed above are the same which is why we will only discuss the fourth optional value that has been used in this new syntax i.e., the built-in binary predicate. This is replaced by a built-in C++ function such as “multiplies” which is then used to calculate the product of the values within the provided range. In this way, the accumulate function in C++ can also be used to serve purposes other than just calculating the sum of the given values.

Examples of Using the Accumulate Function in C++ in Ubuntu 20.04

To help you in understanding the usage of the accumulate function in C++, we have devised the following two examples that will give you a basic overview of this function.

Example # 1: Using the Accumulate Function to Calculate the Sum of a Vector in C++ in Ubuntu 20.04

In this example, we want to calculate the total sum of all the values of a vector in C++. For that, we have implemented the C++ program shown in the image below:

In this example code, you would have noticed that other than the “iostream” header file, we have also included two more header files i.e. “numeric” and “vector”. The reason behind including the “numeric” header file is to facilitate the usage of the accumulate function in C++ whereas the “vector” header file has been included so that we can conveniently deal with the vectors in C++. Then, within our “main()” function, we have defined a vector with the statement “std::vector<int> vect{10, 20, 30}”. We have created an integer type vector with the name “vect” which has three different values i.e., 10, 20, and 30.

Then, we have declared a variable named “sum” in which we want to hold the sum of all the values of our vector. We have equalized this variable to the statement “std::accumulate(vect.begin(), vect.end(), 0)”. In this statement, we have used the accumulate function to calculate the sum of all the values of our vector. “vect.begin()” refers to the starting range of the values to be added whereas “vect.end()” points to the ending range of the values to be added. Moreover, we have kept the initial value of the “sum” variable as “0”. Finally, we have used the “cout” statement to print the value of the “sum” variable on the terminal i.e., the sum of all the values of our vector.

In order to compile this C++ program, we have used the following command:

$ g++ Accumulate.cpp –o Accumulate

Then, for executing this program, we have used the command given below:

./Accumulate

Upon executing this C++ program, the correct value of the sum variable i.e., the sum of all the values of our vector was printed on the terminal as shown in the following image:

Example # 2: Using the Accumulate Function to Calculate the Product of a Vector in C++ in Ubuntu 20.04:

In this example, we want to calculate the total product of all the values of a vector in C++. For that, we have implemented the C++ program shown in the image below:

In this example code, you would have noticed that other than the “iostream” header file, we have also included three more header files i.e., “numeric”, “vector”, and “functional”. The reason behind including the “numeric” header file is to facilitate the usage of the accumulate function in C++ whereas the “vector” header file has been included so that we can conveniently deal with the vectors in C++.

Additionally, the “functional” header file has been included for using the built-in binary predicate “multiplies” for calculating the product of the vector. Then, within our “main()” function, we have defined a vector with the statement “std::vector <int> vect{1, 2, 3}”. We have created an integer type vector with the name “vect” which has three different values i.e. 1, 2, and 3.

Then, we have declared a variable named “product” in which we want to hold the product of all the values of our vector. We have equalized this variable to the statement “std::accumulate(vect.begin(), vect.end(), 1, std::multiplies<int>())”. In this statement, we have used the accumulate function to calculate the product of all the values of our vector. “vect.begin()” refers to the starting range of the values to be multiplied whereas “vect.end()” points to the ending range of the values to be multiplied.

Then, we have kept the initial value of the “product” variable as “1” and the “std::multiplies” predicate has been used to calculate the actual product of all the values of the vector. Finally, we have used the “cout” statement to print the value of the “product” variable on the terminal i.e. the product of all the values of our vector.

We have compiled and executed this C++ code in the same manner as we did in our first example. Upon executing this C++ program, the correct value of the product variable i.e. the product of all the values of our vector was printed on the terminal as shown in the following image:

Conclusion

With the help of this tutorial, we wanted to throw light on the accumulate function in C++ in Ubuntu 20.04. We first introduced you to this function by explaining its purpose and the two different syntaxes with which this function can be used in C++. After that, we practically implemented two different examples that depicted the usage of both these syntaxes of the accumulate function. By going through these two examples, you will learn very well how the accumulate function can be used in C++ for calculating the aggregate of the provided range of values.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.