Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Ignoring the Voices: C++11 Example - Lambda Parameters
#include <functional>
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
double Sum(const vector<double>& values, function<double (double x)> f)
{
double result = 0.0;
for (auto d : values)
{
result += f(d);
}
return result;
}
double Complex(double x)
{
double result = sin(x * 3.14159265);
result -= floor(result);
return result;
}
int main(int argc, char** argv)
{
vector<double> myValues = { 1.3, 2.1, 7.4, 9.6 };
double result1 = Sum(myValues, [](double x) { return x; });
double result2 = Sum(myValues, Complex);
cout << "Result 1 (lambda): " << result1 << endl;
cout << "Result 2 (function): " << result2 << endl;
}
Owner

dazfuller commented Dec 16, 2011

Compiled as follows with GCC 4.6.1 on Ubuntu 11.10

g++ -Wall -Werror -std=c++0x -o lambda_functions lambda_functions.cpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment