Skip to Content
C++ Reactive Programming
book

C++ Reactive Programming

by Praseed Pai, Peter Abraham
June 2018
Intermediate to advanced
348 pages
8h 45m
English
Packt Publishing
Content preview from C++ Reactive Programming

Functors and Lambdas

In classic C++, while using STL, we extensively use Function Objects or Functors by overloading Function Operators to write transformation filters and perform reduction on STL containers:

//----- LambdaThird.cpp#include <iostream>#include <numeric>using namespace std;//////////////////////////// Functors to add and multiply two numberstemplate <typename T>struct addition{    T operator () (const T& init, const T& a ) { return init + a; }};template <typename T>struct multiply {    T operator () (const T& init, const T& a ) { return init * a; }};int main(){    double v1[3] = {1.0, 2.0, 4.0}, sum;    sum = accumulate(v1, v1 + 3, 0.0, addition<double>());    cout << "sum = " << sum << endl; sum = accumulate(v1,v1+3,0.0, [] (const double& ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Functional Programming in C++

Functional Programming in C++

Ivan Cukic

Publisher Resources

ISBN: 9781788629775Supplemental Content