Lambdas并非新概念,在其它语言中已经烂大街了。直接进入主题,先看语法:函数
[ captures ] ( params ) specifiers exception attr -> ret { body } (1) [ captures ] ( params ) -> ret { body } (2) [ captures ] ( params ) { body } (3) [ captures ] { body } (4)
格式 | 意义 |
---|---|
[] | 默认不捕获任何变量 |
[=] | 默认以值捕获全部变量 |
[&] | 默认以引用捕获全部变量 |
[x] | 仅以值捕获x,其它变量不捕获 |
[&x] | 仅以引用捕获x,其它变量不捕获 |
[=, &x] | 默认以值捕获全部变量,可是x是例外,经过引用捕获 |
[&, x] | 默认以引用捕获全部变量,可是x是例外,经过值捕获 |
[this] | 经过引用捕获当前对象(实际上是复制指针) |
[*this] | 经过传值方式捕获当前对象 |
Lambdas重在使用,因此下面直接上实例,由浅入深的介绍使用方法。this
[]{ std::cout<< "hello world!" <<std::endl; }
[]{ std::cout<< "hello world!" <<std::endl; }();
auto l = []{ std::cout<< "hello world!" <<std::endl; }; l();
auto l = [](const std::string &s){ std::cout<< s <<std::endl; }; l("hello world!");
[] -> double{ return 42; }
等价于指针
[]{ return 42; }
若是不指定返回类型,C++11也能够自动推断类型。code
int x = 0; int y = 42; auto f = [x, &y] { std::cout<<"x:" << x << std::endl; std::cout<<"y:" << y << std::endl; ++y; //++x;//Error }; x = y = 77; f(); f(); std::cout<< "final y: " << y <<std::endl;
输出orm
x:0 y:77 x:0 y:78 final y: 79
int x = 0; int y = 42; auto f = [=] { std::cout<<"x:" << x << std::endl; std::cout<<"y:" << y << std::endl; //++y;//Error //++x;//Error }; x = y = 77; f(); f(); std::cout<< "final y: " << y <<std::endl;
输出对象
x:0 y:42 x:0 y:42 final y: 77
int x = 0; int y = 42; auto f = [&] { std::cout<<"x:" << x << std::endl; std::cout<<"y:" << y << std::endl; ++y;//Error ++x;//Error }; x = y = 77; f(); f(); std::cout<< "final x: " << x <<std::endl; std::cout<< "final y: " << y <<std::endl;
输出排序
x:77 y:77 x:78 y:78 final x: 79 final y: 79
std::vector<int> vec = { 1, 2, 3, 4, 5 }; double total = 0; //inclucde 'algorithm' for foreach std::foreach(begin(vec), end(vec), [&](int x) { total += x; }); std::cout<<"total:"<< total <<std::endl;
输出:ci
total:15
struct Point{ double x,y; Point(){ x = (rand() % 10000) - 5000; y = (rand() % 10000) - 5000; } void Print(){ std::cout<<"["<<x<<","<<y<<"]"<<std::endl; } }; int count = 10; std::vector<Point> points; for( auto i = 0; i < 10 ; i++ ) points.push_back(Point()); cout<<"Unsorted:"<<endl; for( auto i = 0; i < 10 ; i++ ) points[i].Print(); std::sort(points.begin(), points.end(), [](const Point& a, const Point& b) -> bool{ return (a.x * a.x) + (a.y * a.y) < (b.x * b.x) + (b.y * b.y); }); cout<<"Sorted:"<<endl; for( auto i = 0; i < 10 ; i++ ) points[i].Print();
输出:作用域
Unsorted: [4383,-4114] [-2223,1915] [2793,3335] [386,-4508] [1649,-3579] [-2638,-4973] [3690,-4941] [2763,-1074] [-4460,-1574] [4172,736] Sorted: [-2223,1915] [2763,-1074] [1649,-3579] [4172,736] [2793,3335] [386,-4508] [-4460,-1574] [-2638,-4973] [4383,-4114] [3690,-4941]
//include<functional> std::function<int(int,int)> returnLambda (){ return [](int x, int y){ return x*y; }; } auto lf = returnLambda(); std::cout<< lf(6,7) << std::endl;
void PerformOperation( function<void()> f ){ f(); } int main(){ int x = 100; auto func = [&](){ x++;}; PerformOperation(func); std::cout<< "x:" << x << std::endl; return 0; }
输出:字符串
x:101