Search
 
SCRIPT & CODE EXAMPLE
 

CPP

sum of elements in c++ stl

accumulate(a.begin(), a.end(), 0)
Comment

Array sum in c++ stl

// C++ program to demonstrate working of accumulate()
#include <iostream> 
#include <numeric>     
using namespace std;
   
// User defined function that returns sum of
// arr[] using accumulate() library function.
int arraySum(int a[], int n) 
{
    int initial_sum  = 0; 
    return accumulate(a, a+n, initial_sum);
}
   
int main() 
{
    int a[] = {5 , 10 , 15} ;
    int n = sizeof(a)/sizeof(a[0]);
    cout << arraySum(a, n);
    return 0;
}
Comment

sum array c++

//Syntax
accumulate(first, last, sum);
accumulate(first, last, sum, myfun); 

first, last : first and last elements of range 
              whose elements are to be added
sum :  initial value of the sum
myfun : a function for performing any 
        specific task. For example, we can
        find product of elements between
        first and last.
//Example
  int a[] = {5 , 10 , 15} ;
  int res = accumulate(a,a+3,0); // 30
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to get double y dividing 2 integers in c++ 
Cpp :: c++ check if string contains non alphanumeric 
Cpp :: sum of stack c++ 
Cpp :: separating class into header and cpp file 
Cpp :: minimum and maximum value of a vector in C++ 
Cpp :: how to get last element of set in c++ 
Cpp :: cannot open include file: 
Cpp :: arduino notone 
Cpp :: structure and function c++ 
Cpp :: to_string c++ 
Cpp :: c++ lcm 
Cpp :: optimized bubble sort 
Cpp :: calling struct to a struct c++ 
Cpp :: cpp bubble sort 
Cpp :: built in led 
Cpp :: 2d vector cpp 
Cpp :: cases in cpp 
Cpp :: initialize whole array to 0 c++ 
Cpp :: c++ 20 struct initialization 
Cpp :: insert vector to end of vector c++ 
Cpp :: height of bst cpp 
Cpp :: return array from function c++ 
Cpp :: armstrong number in cpp 
Cpp :: cpp std list example 
Cpp :: count bits c++ 
Cpp :: remove decimal c++ 
Cpp :: string substr c++ 
Cpp :: C++ Structures (struct) 
Cpp :: how to sort in descending order in c++ 
Cpp :: 58. Length of Last Word leetcode solution in c++ 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =