创建一个匿名函数并执行。Objective-C采用的是上尖号^,而C++ 11采用的是配对的方括号[]。实例如下:

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
 
int main()
{
    []{
        cout << "Hello,Worldn";
    }();
}

我们也可以方便的将这个创建的匿名函数赋值出来调用:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
 
int main()
{
    int i = 1024;
    auto func = [](int i) { // (int i) 是指传入改匿名函数的参数
        cout << i;
    };
    func(i);
}

捕获选项

  • [] Capture nothing (or, a scorched earth strategy?)
  • [&] Capture any referenced variable by reference
  • [=] Capture any referenced variable by making a copy
  • [=, &foo] Capture any referenced variable by making a copy, but capture variable foo by reference
  • [bar] Capture bar by making a copy; don’t copy anything else
  • [this] Capture the this pointer of the enclosing class

[] 不捕获任何变量

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
 
int main()
{
    int i = 1024;
    auto func = [] { cout << i; };
    func();
}

vs 报错
error C3493: 无法隐式捕获“i”,因为尚未指定默认捕获模式
error C2064: 项不会计算为接受 0 个参数的函数

g++ 报错:
error: ‘i’ is not captured

要直接沿用外部的变量需要在 [] 中指名捕获。

[=] 拷贝捕获

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
 
int main()
{
    int i = 1024;
    auto func = [=]{  // [=] 表明将外部的所有变量拷贝一份到该函数内部
        cout << i;
    };
    func();
}

结果:
1024

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
 
int main()
{
    int i = 1024;
    auto fun1 = [=]{
        // fun1 内存在 i
        cout << i; // 1024
        auto fun2 = []{ // 未指名捕获, i 不存在
            cout << i;
        };
        fun2();
    };
    fun1();
}

[&] 引用捕获

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
 
int main()
{
    int i = 1024;
    cout << &i << endl;
    auto fun1 = [&]{
        cout << &i << endl;
    };
    fun1();
}

结果:
0x28ff0c
0x28ff0c

[=, &] 拷贝与引用混合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
 
int main()
{
    int i = 1024, j = 2048;
 
    cout << "i:" << &i << endl;
    cout << "j:" << &j << endl;
 
    auto fun1 = [=, &i]{ // 默认拷贝外部所有变量,但引用变量 i
        cout << "i:" << &i << endl;
        cout << "j:" << &j << endl;
    };
    fun1();
}
1
 

结果
outside i:0x28ff0c
outside j:0x28ff08
inside i:0x28ff0c
inside j:0x28ff04

[bar] 指定引用或拷贝

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
 
int main()
{
    int i = 1024, j = 2048;
 
    cout << "outside i value:" << i << " addr:" << &i << endl;
 
    auto fun1 = [i]{
        cout << "inside  i value:" << i << " addr:" << &i << endl;
        // cout << j << endl; // j 未捕获
    };
    fun1();
}

结果:
outside i value:1024 addr:0x28ff08
inside i value:1024 addr:0x28ff04

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
 
int main()
{
    int i = 1024, j = 2048;
 
    cout << "outside i value:" << i << " addr:" << &i << endl;
 
    auto fun1 = [&i]{
        cout << "inside  i value:" << i << " addr:" << &i << endl;
        // cout << j << endl; // j 未捕获
    };
    fun1();
}

结果:
outside i value:1024 addr:0x28ff08
inside i value:1024 addr:0x28ff08

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
 
int main()
{
    int i = 1024, j = 2048, k;
 
    cout << "outside i:" << &i << endl;
    cout << "outside j:" << &j << endl;
 
    auto fun1 = [i, &j]{
        cout << "inside  i:" << &i << endl;
        cout << "inside  j:" << &j << endl;
        // cout << k; // k 未捕获
    };
    fun1();
}

结果:
outside i:0x28ff0c
outside j:0x28ff08
inside i:0x28ff00
inside j:0x28ff08

[this] 捕获 this 指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
 
class test
{
public:
    void hello() {
        cout << "test hello!n";
    };
    void lambda() {
        auto fun = [this]{ // 捕获了 this 指针
            this->hello(); // 这里 this 调用的就是 class test 的对象了
        };
        fun();
    }
};
 
int main()
{
    test t;
    t.lambda();
}
 
 
https://lellansin.wordpress.com/2014/01/02/c-lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95/

C++ Lambda表达式基本用法(言简意赅,非常清楚)的更多相关文章

  1. python lambda表达式简单用法【转】

    python lambda表达式简单用法 1.lambda是什么? 看个例子: g = lambda x:x+1 看一下执行的结果: g(1) >>>2 g(2) >>& ...

  2. C++11 中function和bind以及lambda 表达式的用法

    关于std::function 的用法:  其实就可以理解成函数指针 1. 保存自由函数 void printA(int a) { cout<<a<<endl; } std:: ...

  3. Lambda 表达式递归用法实例

    注意: 使用Lambda表达式会增加额外开销,但却有时候又蛮方便的. Windows下查找子孙窗口实例: HWND FindDescendantWindows(HWND hWndParent, LPC ...

  4. python lambda表达式简单用法

    习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,即: 1 2 3 4 5 6 7 8 # 普通条件语句 if 1 == 1:     name = 'wupeiqi' else ...

  5. 快速掌握Java中Lambda表达式的用法

    Lambda表达式的作用: Lambda表达式的作用就是简化代码开发,让代码看起来更加简介.它是用来简化匿名内部类的.但是并不是所有的匿名内部类都能用Lambda表达式简化,Lambda表达式是有使用 ...

  6. Lambda表达式的用法

    参考:https://www.cnblogs.com/knowledgesea/p/3163725.html

  7. c++中lambda表达式的用法

    #include <iostream> using namespace std; int main(){ ; auto func1 = [=](;}; auto func2 = [& ...

  8. java8+ Lambda表达式基本用法

    LIST public class LambdaTest { @Getter @Setter @AllArgsConstructor static class Student{ private Lon ...

  9. C# LINQ查询表达式用法对应Lambda表达式

    C#编程语言非常优美,我个人还是非常赞同的.特别是在学习一段时间C#后发现确实在它的语法和美观度来说确实要比其它编程语言强一些(也可能是由于VS编译器的加持)用起来非常舒服,而且对于C#我觉得他最优美 ...

随机推荐

  1. POJ 2402 Palindrome Numbers(LA 2889) 回文数

    POJ:http://poj.org/problem?id=2402 LA:https://icpcarchive.ecs.baylor.edu/index.php?option=com_online ...

  2. 36、ALSA声卡驱动和应用

    (注意:内核上电的时候会把一些没运行的控制器模块的时钟都关掉,所有在写驱动的时候需要在使用的使用使用clk_get和clk_enable使能时钟) (说明:与ALSA声卡对应的是OSS架构,第二期视频 ...

  3. python类继承时构造函数的运行问题

    假设子类定义了自己的__init__构造方法函数.当子类的实例对象被创建时,子类仅仅会运行自己的__init__方法函数.假设子类没有定义自己的构造方法函数.会沿着搜索树找到父类的构造方法函数去运行父 ...

  4. [D3] Create Labels from Non-numeric Data with Ordinal Scales in D3 v4

    When your data contains discrete, non-numeric property values that you need to format or convert bef ...

  5. 【翻译自mos文章】Clusterware间歇性的hang,命令报CRS-184而且Network Socket Files in /tmp/.oracle or /var/tmp/.oracle被删

    来源于: Clusterware Intermittently Hangs And Commands Fail With CRS-184 as Network Socker Files in /tmp ...

  6. 【Nutch2.2.1基础教程之3】Nutch2.2.1配置文件 分类: H3_NUTCH 2014-08-18 16:33 1376人阅读 评论(0) 收藏

    nutch-site.xml 在nutch2.2.1中,有两份配置文件:nutch-default.xml与nutch-site.xml. 其中前者是nutch自带的默认属性,一般情况下不要修改. 如 ...

  7. php 模拟get提交

    方法一: $re = file_get_contents($url); print_r($re); 方法二: $ch = curl_init("http://www.jb51.net/&qu ...

  8. #import &lt;/usr/include/objc/objc-class.h&gt; not such file or directory问题的解决方法

    近期在使用一些开源的demo,打开后出现这个错误,然后能够把 #import </usr/include/objc/objc-class.h> 改动为以下 #import <objc ...

  9. android Navigator的高度计算和推断是否显示

    进入互联网行业几天了, 从手机行业转到互联网行业也在慢慢的适应: IDE工具的使用(之前一直在Ubuntu 命令行进行开发). 版本号管理工具,代码架构等等这些都须要又一次适应. 好在本人另一些底子, ...

  10. 【机器学习实战】第4章 朴素贝叶斯(Naive Bayes)

    第4章 基于概率论的分类方法:朴素贝叶斯 朴素贝叶斯 概述 贝叶斯分类是一类分类算法的总称,这类算法均以贝叶斯定理为基础,故统称为贝叶斯分类.本章首先介绍贝叶斯分类算法的基础——贝叶斯定理.最后,我们 ...