forked from Drishty06/STL-Functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccumulate.cpp
46 lines (41 loc) · 1.45 KB
/
accumulate.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
// Used for : Usually used to find sum of array or vector.
// Syntax 1 : accumulate(first, last, sum);
// first, last : Iterator to first and last elements of range whose elements are to be added
// sum : initial value of the sum
// Syntax 2 : accumulate(first, last, sum, myfun);
// myfun : a function for performing any specific task. For example, we can find product between first and last.
// Code :
#include <iostream>
#include <numeric>
using namespace std;
// User defined function
int myfun(int x, int y)
{
return x * y ;
}
int main()
{
// Initialize sum = 1
int sum = 1;
int a[] = {5 , 10 , 15} ;
cout << "\nResult using accumulate: ";
cout << accumulate(a , a+3 , sum); //prints 31 (1+5+10+15 = 31)
// Using accumulate function with
// defined function
cout << "\nResult using accumulate with"
"user-defined function: ";
cout << accumulate(a, a+3, sum, myfun); //prints 750 (1*5*10*15 = 750)
// Using accumulate function with
// pre-defined function
cout << "\nResult using accumulate with "
"pre-defined function: ";
cout << accumulate(a, a+3, sum, std::minus<int>()); // prints -29 (1-5-10-15 = -29)
return 0;
}
// For More Info : https://www.geeksforgeeks.org/accumulate-and-partial_sum-in-c-stl-numeric-header/