<七>lambda表达式实现原理

C++11 函数对象的升级版=>lambda表达式

函数对象的缺点:
使用在泛型算法,参数传递, 比较性质/自定义操作 优先级队列, 需要专门定义出一个类

//lambda&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;:
//[&#x6355;&#x83B7;&#x5916;&#x90E8;&#x53D8;&#x91CF;](&#x5F62;&#x53C2;&#x5217;&#x8868;)->&#x8FD4;&#x56DE;&#x503C;{&#x64CD;&#x4F5C;&#x4EE3;&#x7801;}

auto func1=[]()->void{cout<<"hello world"<<endl;} func1(); 编译器根据 []()->void{cout<<"hello world"<<endl;} 产生一个函数对象 < code></"hello></"hello>

上面等价于

template<typename t="void">
class TestLamda{

public:
    TestLamda(){}
    void operator(){
        cout<<"hello world"<<endl; } testlamda<> t1;
t1();
</"hello></typename>
[]()->void{cout<<"hello world"<<endl;} [] 相当于 testlamda(){} () operator() void {cout<<"hello operator(){ cout<<"hello world"<<endl; } < code></"hello>

示例2

auto f2=[](int a,int b)->int {int c=a+b;cout<<c<<endl; return c;} 相当于 template<typename t="int">
class TestLamda{

public:
   TestLamda(){}
    int operator(int a, int b){
        int c= a+b;
        cout<<c<<endl; return c; } < code></c<<endl;></c<<endl;>

如果lambda表达式没有返回值那么 “->返回值” 可以不要 优化如下
auto f2=[](int a,int b){int c=a+b;cout<<c<<endl; return c;}< code></c<<endl;>

&#x5173;&#x4E8E; [&#x6355;&#x83B7;&#x5916;&#x90E8;&#x53D8;&#x91CF;]
[]&#x8868;&#x793A;&#x4E0D;&#x6355;&#x83B7;&#x4EFB;&#x4F55;&#x53D8;&#x91CF;
[=] &#x4EE5;&#x4F20;&#x503C;&#x5F97;&#x65B9;&#x5F0F;&#x6355;&#x83B7;&#x5916;&#x90E8;&#x7684;&#x6240;&#x6709;&#x53D8;&#x91CF;
[&] &#x4EE5;&#x4F20;&#x5F15;&#x7528;&#x7684;&#x65B9;&#x5F0F;&#x6355;&#x83B7;&#x5916;&#x90E8;&#x6240;&#x6709;&#x53D8;&#x91CF;
[this] &#x6355;&#x83B7;&#x5916;&#x90E8;&#x7684;this&#x6307;&#x9488;
[=,&a] &#x4EE5;&#x4F20;&#x503C;&#x7684;&#x65B9;&#x5F0F;&#x6355;&#x83B7;&#x5916;&#x90E8;&#x7684;&#x6240;&#x6709;&#x53D8;&#x91CF;,&#x4F46;&#x662F;a&#x53D8;&#x91CF;&#x4EE5;&#x4F20;&#x5F15;&#x7528;&#x7684;&#x65B9;&#x5F0F;&#x6355;&#x83B7;
[a,b]  &#x4EE5;&#x503C;&#x4F20;&#x9012;&#x7684;&#x65B9;&#x5F0F;&#x6355;&#x83B7;&#x5916;&#x90E8;a &#x548C; b
[a,&b] &#x4EE5;&#x503C;&#x4F20;&#x9012;&#x7684;&#x65B9;&#x5F0F;&#x6355;&#x83B7;a, b&#x4EE5;&#x5F15;&#x7528;&#x7684;&#x65B9;&#x5F0F;&#x4F20;&#x9012;

int a=10;
int b=20;
auto function=[&a,&b](){int temp=a;a=b;b=temp;} //&#x5B9E;&#x73B0;a,b&#x4EA4;&#x6362;, &#x4E00;&#x5B9A;&#x8981; [&a,&b] &#x5F15;&#x7528; &#x6216;&#x8005;
auto function=[&](){int temp=a;a=b;b=temp;}// &#x8868;&#x793A;&#x5B9A;&#x4E49;&#x7684;&#x5916;&#x90E8;&#x53D8;&#x91CF;&#x5168;&#x90E8;&#x4EE5;&#x5F15;&#x7528;&#x7684;&#x65B9;&#x5F0F; &#x4F20;&#x5165;,&#x6765;&#x6355;&#x83B7;.

 int x = 200;
 int y = 100;

 std::function<void()> f= [&x, &y]()->void{x = x + 1; y = y + 1;};

 f();

 std::cout << x << "=" << y << std::endl;

</void()>

lambda 简单应用

vector<int> v1;

//&#x6392;&#x5E8F;
sort(v1.begin(),v1.end(),[](int a, int b)->bool{return a>b;})

//&#x627E;&#x5230; &#x7B2C;&#x4E00;&#x4E2A;&#x5C0F;&#x4E8E;65&#x7684; &#x503C;
auto it_find=find_if(v1.begin(),v1.end(),[](int a)->bool{return a<65;}) 打印元素 for_each( ;v1.begin()!="v1.end()" ; [](int a){cout<<a<<endl;}) 打印偶数 a){ if(a%2="=0){" cout<<a<<endl; } }) < code></65;})></int>

Original: https://www.cnblogs.com/erichome/p/16966411.html
Author: Hello_Bugs
Title: <七>lambda表达式实现原理

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/796962/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球