Skip to content

Instantly share code, notes, and snippets.

@alibitek
Created August 13, 2012 19:14
Show Gist options
  • Save alibitek/3343393 to your computer and use it in GitHub Desktop.
Save alibitek/3343393 to your computer and use it in GitHub Desktop.
C++ Lambda Expressions
// Lambda Expressions
std::vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
std::for_each(v.rbegin(), v.rend(), [](int n) { std::cout << n << " "; });
std::cout << std::endl;
std::cout << std::endl << "------------------------" << std::endl;
for_each(v.begin(),
v.end(),
[](int n)
{
std::cout << n << " ";
});
std::cout << std::endl;
std::cout << std::endl << "------------------------" << std::endl;
std::for_each(v.begin(), v.end(), [](int n)
{
std::cout << n;
if (n % 2 == 0)
{
std::cout << " even " << std::endl;
}
else
{
std::cout << " odd " << std::endl;
}
});
std::cout << std::endl << "------------------------" << std::endl;
std::vector<int> v2;
std::transform(v.begin(), v.end(), std::back_inserter(v2), [](int n) { return n * n * n; });
for_each(v2.begin(), v2.end(), [](int n) { std::cout << n << " "; });
std::cout << std::endl;
std::cout << std::endl << "------------------------" << std::endl;
// explicitly specify the return type
std::vector<double> dv;
std::transform(v.begin(), v.end(), std::back_inserter(dv), [](int n) -> double {
if (n % 2 == 0)
{
return n * n * n;
}
else
{
return n / 2.0;
}
});
for_each(dv.begin(), dv.end(), [](double n) { std::cout << n << " "; });
std::cout << std::endl;
// Lambda captures (use variables from outside the lambda expression or its paramters)
// capturing by value explicitly [x, y]
int x = 3;
int y = 7;
std::cout << "Printing elements between " << x << " and " << y << " inclusive" << std::endl;
std::for_each(v.begin(), v.end(), [x, y](int n)
{
if (n >= x && n <= y)
{
std::cout << n << " ";
}
});
std::cout << std::endl;
std::for_each(v.begin(), v.end(), [](int n) { std::cout << n << " "; });
std::cout << std::endl;
// capturing by value explicitly [=] (Everything that is in scope and used in the annonymous funciton)
x = 2;
y = 9;
std::cout << "Printing elements between " << x << " and " << y << " inclusive" << std::endl;
std::for_each(v.begin(), v.end(), [=](int n)
{
if (n >= x && n <= y)
{
std::cout << n << " ";
}
});
std::cout << std::endl;
std::for_each(v.begin(), v.end(), [](int n) { std::cout << n << " "; });
std::cout << std::endl;
// capturing by value and changing locally [=] mutable
x = 1;
y = 1;
for_each(v.begin(), v.end(), [=](int& r) mutable {
const int old = r;
r *= 2;
x = y;
y = old;
});
std::cout << "doubling" << std::endl;
std::for_each(v.begin(), v.end(), [](int n) { std::cout << n << " "; });
std::cout << std::endl;
std::cout << "x,y => " << x << "," << y << std::endl;
std::cout << std::endl << "--------------------" << std::endl;
// capturing by reference [&x, &y] - also changing the param (int&)
v.clear();
for (int i = 0; i < 10; ++i) { v.push_back(i); }
std::for_each(v.begin(), v.end(), [&x, &y](int& r) {
const int old = r;
r *= 2;
x = y;
y = old;
});
for_each(v.begin(), v.end(), [](int n) { std::cout << n << " "; });
std::cout << std::endl;
std::cout << "x,y => " << x << "," << y << std::endl;
std::cout << std::endl << "--------------------" << std::endl;
// capturing whole stack by reference (and a neater way to keep initializing)
v.clear();
int i = 0;
generate_n(back_inserter(v), 10, [&] { return i++; }); // equivalent to [&]() { return i++; }
std::cout << "initializing" << std::endl;
for_each(v.begin(), v.end(), [](int n) { std::cout << n << " "; });
std::cout << std::endl;
std::cout << "i: " << i << std::endl;
std::cout << std::endl << "--------------------" << std::endl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment