forked from Drishty06/STL-Functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartial_sum.cpp
48 lines (43 loc) · 1.36 KB
/
partial_sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Used for : Usually used to find prefix sum array
// Syntax 1 : partial_sum(first, last, b);
// first, last : Iterator to first and last elements of range whose elements are to be added
// b : array or vector in which you want to store modified array or vector.
// Syntax 2 :partial_sum(first, last, b, myfun);
// myfun : a function for performing any specific task. For example, we can store product of 2 continuous value.
// Code :
#include <iostream>
#include <numeric>
using namespace std;
//user defined function
int myfun(int x, int y)
{
// the sum of element is twice of its
// adjacent element
return x + 2 * y;
}
int main ()
{
int a[] = {1, 2, 3, 4, 5} ;
int b[5];
// Default function
partial_sum(a , a+5 , b);
cout << "Partial Sum - Using Default function: ";
for (int i=0; i<5; i++)
cout << b[i] << ' '; // 1 3 6 10 15
cout << '\n';
// Using user defined function
partial_sum(a , a+5 , b , myfun) ;
cout << "Partial sum - Using user defined function: ";
for (int i=0; i<5; i++)
cout << b[i] << ' '; // 1 5 11 19 29
cout << '\n';
return 0;
}
// For More Info : https://www.geeksforgeeks.org/accumulate-and-partial_sum-in-c-stl-numeric-header/