Array sum in C++ STL



The array sum refers to the sum of all elements in an array. The STL library of C++ provides built-in functions to easily calculate the sum of elements in an array. In this article, we will explain all those STL functions with examples.

Consider the following input/output scenario to understand the concept of array sum:

Input: 
int arr[] = {1, 2, 3, 4, 5};

Output:
15

Explanation: The sum of the elements in the array is 1 + 2 + 3 + 4 + 5 = 15.

Following are the different STL functions to find the array sum in C++:

Finding Array Sum Using accumulate() Function

The accumulate is used to accumulate the values in a range defined by two iterators. By default, it will calculate the sum of the elements in the range. (i.e., this function can also be used to find the product or other operations.) It is defined in the numeric header file.

Syntax

#include <numeric>

std::accumulate(first, last, sum);

Where first is the starting iterator, last is the ending iterator of the array, and sum is the initial value for sum (usually 0).

Example

The following example demonstrates how to use the accumulate() function in C++ program.

#include <iostream>
#include <numeric>
using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    // Using accumulate to find the sum
    int sum = accumulate(arr, arr + n, 0);

    cout << "Sum of array elements: " << sum << endl;

    return 0;
}

The output of the program will be:

Sum of array elements: 15

Finding Array Sum Using reduce() Function

The reduce() function is another STL function that reduces a range of elements to a single value. By default, it will calculate the sum of the elements in the range. Similar to the accumulate() function, it is also defined in the numeric header file. The main difference here is that reduce() is suitable for large datasets and can be used with parallel execution policies.

Syntax

#include <numeric>

std::reduce(first, last, sum);

Where first is the starting iterator, last is the ending iterator of the array, and sum is the initial value (usually 0).

Example

The following example demonstrates how to use the reduce() function in a C++ program.

#include <iostream>
#include <numeric>
using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    // Using reduce to find the sum
    int sum = reduce(arr, arr + n, 0);

    cout << "Sum of array elements: " << sum << endl;

    return 0;
}

The output of the program will be:

Sum of array elements: 15

Conclusion

We have discussed two STL functions, accumulate() and reduce(), to find the sum of elements in an array in C++. Both functions have similar syntax and functionality, but reduce() is more suitable in the case of large datasets due to its ability to work with parallel execution policies. You can choose either based on your specific requirements.

Updated on: 2025-07-21T18:59:36+05:30

36K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements