accumulate() and partial_sum() in C++ STL : Numeric header17 Mar 2025 | 3 min read What is C++ STL?In C++, we have STL, which is also called Standard Template Library, which has a lot of inbuilt functions implemented, and we can use them directly by just importing the library. In the same way, we have a numeric library in the STL, and the numeric header is the part of the numeric library. The numeric header has a lot of inbuilt functions used for mathematical calculation, which saves a lot of time for the programmer. For example:
We will discuss the following functions in this article: 1. accumulate()If we want to get the sum of elements of an array in a particular range, then we can directly use this function .Otherwise we have to use a loop to traverse the array and then we get the sum. There are two ways of using this function: i) With three argumentsIn this type, this function will take three arguments: starting position, ending position and a variable sum, which will add its initial value to the sum of elements in that range. Syntax: C++ Example: Output: ![]() Explanation In the above code, we have sum_variable initialized with value 12. Now we have an array of 9 elements, and we are using the function from arr+0 to arr+3. It means it will add the values from index 0 to index 2 (the last position index is excluded), and then it will add the value of sum_variable to the sum we got. So the sum of index 0 to index 2 is six, and we will add 12, so it will print value 18. ii) With four argumentsIn this function, we can use the fourth argument as another that we want to run while running this function. Syntax: C++ Example: Output: ![]() Explanation: In the above code, we created a mult function which returns the multiplication value of two numbers. So we will multiply the values from index 0 to index three, which will be 24, and then this result will be multiplied again to our sum_variable, so it will print the value of 24x12, which will be 288. 1. partial_sum()If we want to get the partial sum of an array from a range and store this result in another array, then we can easily use this function. We can use this function in two ways: i) With three argumentsIn this type, we will be having three arguments: one is the starting position, the second is the last index which is excluded, and the third one is the array in which we want to store the result. Syntax: Suppose we have n elements in an array, and the index will be from 0 to n-1. So if we want to get the partial sum from index L to index R, then we will create the resultant array of size R-L+1, and the values in the resultant array will be stored as follows: Let res[] is the resultant array then: C++ Example: Output: ![]() Explanation: In the above example, we will get the partial sum of index 2 to index five, and we will store these four values in the array b. So b[0]=arr[2] = 3 b[1]=arr[2]+arr[3]=7 b[2]=arr[2]+arr[3]+arr[4]=12 b[3]=arr[2]+arr[3]+arr[4]+arr[5]=18 ii) With four argumentsWe can use the fourth argument as another function where we can define our own definition of the partial sum. Syntax: C++ Example: Output: ![]() Explanation: In the above code, we have our own function so that we will get output in the resultant array according to our own function. b[0]=arr[2] =3 b[1]=2*arr[3]-b[0]=5 b[2]=2*arr[4]-b[1]=5 b[3]=2*arr[5]-b[2]=7 Next TopicArrays of Vectors in C++ STL |
The Great Tree List Recursion Problem in C++
In this article, we will discuss the great tree list problem in C++ with several examples. Introduction: Think of a program that determines a number's factorial. This function takes a number N as an input and returns the factorial of N as a result. This function's pseudo-code will...
7 min read
QuickSort on Singly Linked List in C++
Sorting is a fundamental operation in computer science and finds its epitome in QuickSort. Quicksort is a divide-and-conquer algorithm renowned for its efficiency. Extending QuickSort to linked lists is a valuable skill, though it is conventionally applied to arrays. In this article, we delve into the...
5 min read
Applications of C++
In this article, we will discuss the applications of C++. The computer language C++ is flexible and has many uses in a variety of industries. Some of the most popular C++ programs are listed below: System software development: C++ is frequently used to create system-level software, such as...
3 min read
KMP Algorithm in C++
In this tutorial, we will study the KMP algorithm in C++ with code implementation. Other algorithms used for pattern matching include the Naive Algorithm and the Rabin Karp Algorithm. If we compare the algorithms, the Naive method and Rabin Karp have a time complexity of O((n-m)*m);...
9 min read
conj() function in C++
The is present in the standard library. C++ provides a wide range of inbuilt functions for complex numbers. It is also a built-in function that deals with complex numbers. This function is provided by the <comple> header file. The main intention of this function is...
3 min read
Naming Conventions in C++
C++ has a set of rules for naming variables, functions, and other identifiers in your code. These rules, known as naming conventions, help to make your code more readable and maintainable. Guidelines for Variable names should be descriptive and meaningful. For example, a variable that holds the...
9 min read
Std::allocator() in C++
Efficient memory management is paramount in the world of C++, where building resilient and high-performance applications hinges on optimal resource utilization. At the core of this endeavor lies the std::allocator class, a foundational element for dynamic memory allocation. In this article, we embark on a journey...
4 min read
C++ hashing programme with chaining
What exactly is hash table chaining? Chaining is a hash table collision avoidance technique. A collision occurs when two keys in a hash table are hashed to the same index. Collisions are an issue because each slot in a hash table is only supposed to hold one element. The...
9 min read
Set insertion and deletion in C++
A Set is a container built in C++ with a concept comparable to the set described in mathematics. The set differs from other containers in that it includes only distinct elements, which may be traversed in sorted order. A good understanding of sets is important in...
6 min read
Boost::split in c++ library
Strtok in C is comparable to this function. Tokens are created from the input sequence and are separated by separators. Through the predicate, separators are provided. Syntax: Template: split(Result, Input, Predicate Pred); Parameters: Input: A container which will be searched. Pred: A predicate to identify separators. This predicate is supposed to return...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India



