0% found this document useful (0 votes)
18 views1 page

LAMBDA Demo 03 Accumulate - CPP

The document is a C++ program demonstrating the use of the std::accumulate function with different methods of accumulation. It showcases the default behavior, the use of std::minus, and custom functions and function objects for accumulation. The program initializes an array of numbers and accumulates their values starting from an initial value of 100.

Uploaded by

rockyyyy884
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views1 page

LAMBDA Demo 03 Accumulate - CPP

The document is a C++ program demonstrating the use of the std::accumulate function with different methods of accumulation. It showcases the default behavior, the use of std::minus, and custom functions and function objects for accumulation. The program initializes an array of numbers and accumulates their values starting from an initial value of 100.

Uploaded by

rockyyyy884
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

// accumulate example

#include <iostream> // std::cout


#include <functional> // std::minus
#include <numeric> // std::accumulate
int myfunction (int x, int y)
{ return x + 2*y;
}
class myclass
{ public:
int operator()(int x, int y)
{ return x + 3*y;
}
};

int main ()
{ int init = 100;
int numbers[] = {10,20,30};

std::cout << "using default accumulate: ";


std::cout << std::accumulate( numbers,
numbers+3,
init);

std::cout << '\n';


std::cout << "using functional's minus: ";

std::cout << std::accumulate (numbers,


numbers+3,
init,
std::minus<int>() );

std::cout << '\n';


std::cout << "using custom functional function: ";
std::cout << std::accumulate (numbers,
numbers+3,
init,
myfunction);

std::cout << '\n';

std::cout << "using custom functional object: ";


myobject
std::cout << std::accumulate (numbers, numbers+3, init, myobject);
std::cout << '\n';
return 0;
}

You might also like