Javatpoint Logo
Javatpoint Logo

accumulate() and partial_sum() in C++ STL : Numeric header

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:

  1. accumulate()
  2. partial_sum()
  3. iota
  4. inner_product
  5. reduce
  6. range
  7. incluse_scan()
  8. exclusive_scan()

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 arguments

In 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:

accumulate() and partial_sum() in C++ STL : Numeric header

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 arguments

In this function, we can use the fourth argument as another that we want to run while running this function.

Syntax:

C++ Example:

Output:

accumulate() and partial_sum() in C++ STL : Numeric header

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 arguments

In 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:

accumulate() and partial_sum() in C++ STL : Numeric header

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 arguments

We can use the fourth argument as another function where we can define our own definition of the partial sum.

Syntax:

C++ Example:

Output:

accumulate() and partial_sum() in C++ STL : Numeric header

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







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA