Table of contents
1.
Introduction
2.
What is  C++ Accumulate?
2.1.
Syntax of C++ accumulate() function
2.2.
Parameters of C++ accumulate() function
2.3.
Return Type of C++ accumulate() function
3.
Example of C++ Accumulate Function
3.1.
Example 1
3.1.1.
Code
3.2.
C++
3.2.1.
Output
3.2.2.
Code
3.3.
C++
3.3.1.
Output
3.4.
Example 2
3.4.1.
Code
3.5.
C++
3.5.1.
Output
3.5.2.
Code
3.6.
C++
3.6.1.
Output
4.
Frequently Asked Questions
4.1.
What does accumulate do in C++?
4.2.
What does std :: accumulate do?
4.3.
What is accumulate used for?
5.
Conclusion
Last Updated: Aug 26, 2024
Easy

C++ Accumulate

Author Aayush Sharma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Clarity and efficiency are two of the most important elements in judging the quality of programming code. It becomes easy for developers to understand each other's code if their code is written clearly. This enhances developer productivity and frees up valuable time. Many built-in functions in C++ allow users to execute multiple extended operations with only one function call. One such function is accumulate in C++.

c++ accumulate

In this blog, we will discuss the C++ Accumulate function in detail. We will look at the syntax, parameters, and return type of the Accumulate function in C++. We will also look at some examples of C++ Accumulate with their suitable code implementations. In the end, we will conclude by discussing some frequently asked questions.

What is  C++ Accumulate?

C++ Accumulate is a very useful function present in the C++ Standard Template library. C++ Accumulate is used to optimize aggregation tasks and make them more simple, shorter, and clearer to understand. C++ Accumulate is designed to calculate the sum of values stored in a container in just a few words as compared to manually adding them one by one using conditional loops.

C++ Accumulate is used to iterate over a sequence of elements generally stored in a container like an array or vector and accumulate their values using an operation. This operation can also be defined by the user to perform a more complex operation on the numbers.

The C++ Accumulate function is not only useful for numerical data types. It can be applied to almost any data type. C++ Accumulate can be used for various scenarios like aggregating values, calculating averages, operating on custom data types, etc. C++ accumulates code readability and cleaner code with its minimal and easy-to-understand syntax, which we will discuss in the next section.

Syntax of C++ accumulate() function

accumulate (starting range, ending range, initial value);

In the above syntax, the start and end ranges determine the scope of calculation, and the initial value is to initialize the return value with it.

Parameters of C++ accumulate() function

The accumulate() function takes three parameters:

  • Starting range - It is an iterator representing the starting position of the values to be operated on.
     
  • Ending range - It is an iterator representing the ending position of the values to be operated on.
     
  • Initial value - This value represents the starting value given to the accumulated function. For example, if we want to calculate the total sum of an array, we will pass this as 0. But if we wish for our sum to start from a particular value, we can also pass that value.

Return Type of C++ accumulate() function

The return type of the accumulate() function is determined by the data type in which we store the return value.

Also see, Abstract Data Types in C++

Example of C++ Accumulate Function

Now that we know about the Syntax, Parameters, and Return types of the C++ Accumulate function, we will look at some examples to understand the usage and working of the Accumulate function.

Example 1

Suppose we want to calculate the sum of the values in a vector initialized with 0. One way to do this is to use a for loop to calculate it.

Code

  • C++

C++

#include <bits/stdc++.h>
using namespace std;

int main(){

// vector containing the values to be added
vector<int>numbers={1,2,3,4,5};

// initializing the sum with 0
int sum=0;

// iterating on all the numbers in the vector
for(auto it:numbers){
sum+=it;
}

// printing the answer
cout<<sum<<endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

output

Now suppose we want to do it with the help of the accumulate function.

Code

  • C++

C++

#include <bits/stdc++.h>
using namespace std;

int main(){

// vector containing the values to be added
vector<int>numbers={1,2,3,4,5};

// using the accumulate function to calculate the sum
int sum=accumulate(numbers.begin(), numbers.end(), 0);

// printing the answer
cout<<sum<<endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

output

Example 2

Now let us find the product of the vector using both the for loop and the accumulate function.

Code

  • C++

C++

#include <bits/stdc++.h>
using namespace std;

int main(){

// vector containing the values to be added
vector<int>numbers={1,2,3,4,5};

// initializing the prod with 1
int prod=1;

// iterating on all the numbers in the vector
for(auto it:numbers){
prod*=it;
}
// printing the answer
cout<<prod<<endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

output

Now let us calculate the product using the accumulate function

Code

  • C++

C++

#include <bits/stdc++.h>
using namespace std;

int main(){

// vector containing the values to be added
vector<int>numbers={1,2,3,4,5};

// using the accumulate function to calculate the product
int prod=accumulate(numbers.begin(), numbers.end(), 1, multiplies<int>());

// printing the answer
cout<<prod<<endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

output

Frequently Asked Questions

What does accumulate do in C++?

The accumulate function in C++ sums up a range of elements, returning the total. It starts with an initial value and applies a binary operation (typically addition) to combine the elements sequentially.

What does std :: accumulate do?

The std::accumulate function, found in the C++ Standard Library, adds elements within a specified range. It begins with an initial value and iteratively applies an operation (default is addition) to compute the final result.

What is accumulate used for?

The accumulate function is used for calculating the sum or another aggregated result (like multiplication) of elements within a container, such as an array or vector, by applying a specific binary operation.

Conclusion

In this article, we discussed C++ Accumulate. We discussed the C++ Accumulate function's syntax, parameters, and return type. We also discussed an example of the C++ Accumulate function with its code implementation. In the end, we concluded by discussing some frequently asked questions related to the topic.

So now that you know about C++ Accumulate, you can refer to similar articles.

You may refer to our Guided Path on Code360 for enhancing your skill set.

Live masterclass