In this tutorial, we will look at the accumulation function and how to use it efficiently in C++ programs.
What is C++ accumulate() Function
The accumulate() function simplifies the process of finding the sum or combining elements in a range specified by ‘first’ and ‘last’ iterators. It eliminates the need for a linear operation that adds each value in the range one by one and saves the result in a variable.
In C++, the accumulate() function adds all elements within a range by default. However, we can also specify a different binary operation using a function pointer or lambda expression as a third argument. This feature makes the accumulation function extremely adaptable, allowing it to be used for purposes other than basic addition, such as calculating the product of a range of values.
Syntax for C++ accumulate() Function
The syntax for using the accumulate() function is:
Parameters: The C++ accumulate() function accepts three parameters:
- start_val: This is the starting position of an iterator.
- end_val: This is the last position of an iterator.
- initial_value: This is the initial value used for accumulation.
The accumulate() function in C++ can also accept an additional parameter to specify the type of operation to be performed. The syntax for the accumulate function with an additional argument is as follows:
Here, the operation is an additional argument that specifies the operation to be performed.
Return Value of C++ accumulate() Function
The return value of the accumulate function is the result of the performed operation on all the values in the specified range [first, last] accumulated to the initial value provided.
Examples
As discussed earlier, there are multiple operations you can do with accumulate() function, and some of them are given below:
Example 1: Using C++ accumulate() Function to Sum up a Range of Integers
You can use the accumulate() function to sum up a range of integers in C++ and the code for such a scenario is provided below:
#include <numeric>
#include <vector>
using namespace std;
int main() {
vector<int> numbers = {1, 2, 3, 4, 5};
int sum = accumulate(numbers.begin(), numbers.end(), 0);
cout << "The sum is: " << sum << endl;
return 0;
}
The above code showcases the usage of the accumulate() function that calculates the sum of elements in vector numbers and prints the result to the console using cout. In the above code, the initial value is 0.
Output
Example 2: Using C++ accumulate() Function with a Custom Function
You can also use the accumulate() function with a custom function to find the product of elements in a range and the code is given below:
#include <numeric>
#include <vector>
using namespace std;
int customFunc(int a, int b) {
return a * b;
}
int main() {
vector<int> v = {1, 2, 3, 4, 5};
int product = accumulate(v.begin(), v.end(), 1, customFunc);
cout << "Product of the vector is: " << product << endl;
return 0;
}
This above code demonstrates the usage of the accumulate() function of C++. It calculates the product of elements in a vector v using a custom multiplication function customFunc(). The cout is then used to print the result to the console.
Output
Example 3: Using C++ accumulate() Function to Concatenate Strings
Another advantage of using accumulate function is to easily concatenate strings in a vector, and the example for such a case is given below:
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main() {
vector<string> v = {"Hello", ", ", "LinuxHint", "!"};
string result = accumulate(v.begin(), v.end(), string(""));
cout << "Concatenated string: " << result << endl;
return 0;
}
The above program creates a vector of strings v and concatenates them using the accumulate() function with an empty string as the initial value. The cout is then used to print the resulting concatenated string to the console.
Output
Conclusion
The accumulate() function can be used to conduct numerical operations on value ranges. It simplifies the process of finding the sum or product of elements in a range and eliminates the need for manual iteration. The function is highly adaptable, also allowing for custom operations to be performed on the range.