This header is part of the numeric library in C++ STL. This article explains some useful functions in the numeric header which can be used during competitive programming to save time and effort.
We usually find out the sum of elements in a particular range or a complete array using a linear operation which requires adding all the elements in the range one by one and storing it into some variable after each iteration.
This function returns the sum of all the values lying in a range between [first, last) with the variable sum.
- Syntax 1:
accumulate(first, last, sum); first, last : first and last elements of range whose elements are to be added sum : initial value of the sum
- Syntax 2: This function returns the sum of all the values lying between [first, last) with the variable sum.
accumulate(first, last, sum, myfun); myfun : a function for performing any specific task. For example, we can find product of elements between first and last.
Result using accumulate: 31 Result using accumulate with user-defined function: 750 Result using accumulate with pre-defined function: -29
An Example Problem : Sum of all elements between k1’th and k2’th smallest elements
This function assigns partial sum of the corresponding elements of an array to every position of the second array.It returns the partial sum of all the set of values lying between [first, last) and stores it in another array b.
For example, if x represents an element in [first, last) and y represents an element in result, the ys can be calculated as:
y0 = x0 y1 = x0 + x1 y2 = x0 + x1 + x2 y3 = x0 + x1 + x2 + x3 y4 = x0 + x1 + x2 + x3 + x4
partial_sum(first, last, b); partial_sum(first, last, b, myfun); first, last : first and last element of range whose elements are to be added b : index of array where corresponding partial sum will be stored; myfun : a user defined function for performing any specific task
Partial Sum - Using Default function: 1 3 6 10 15 Partial sum - Using user defined function: 1 5 11 19 29
This article is contributed by Abhinav Tiwari .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
- <numeric> library in C++ STL
- Namespace in C++ | Set 3 (Accessing, creating header, nesting and aliasing)
- What’s difference between header files "stdio.h" and "stdlib.h" ?
- random header in C++ | Set 1(Generators)
- random header | Set 2 (Distributions)
- Print "Hello World" in C/C++ without using any header file
- random header in C++ | Set 3 (Distributions)
- clocale header file in C++
- Exception header in C++ with examples
- Header files in C/C++ with Examples
- ASCII NUL, ASCII 0 ('0') and Numeric literal 0
- Split numeric, alphabetic and special symbols from a String
- fill() and fill_n() functions in C++ STL
- Array algorithms in C++ STL (all_of, any_of, none_of, copy_n and iota)
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)
- Descending order in Map and Multimap of C++ STL
- std::upper_bound and std::lower_bound for Vector in C++ STL
- Insertion and Deletion in STL Set C++