C++11中lambda表达式的基本语法格式为:

[capture](parameters) -> return_type { /* ... */ }

  其中 [] 内为外部变量的传递方式:

[]        //no variables defined. Attempting to use any external variables in the lambda is an error.
[x, &y] //x is captured by value, y is captured by reference
[&] //any external variable is implicitly captured by reference if used
[=] //any external variable is implicitly captured by value if used
[&, x] //x is explicitly captured by value. Other variables will be captured by reference
[=, &z] //z is explicitly captured by reference. Other variables will be captured by value

  () 内为参数,比如:

[](int x, int y) -> int { return x + y; }

  return_type就是字面上的意思,也就是返回类型或者说函数类型。{}内则是我们编写的函数要执行的功能代码。

  我们知道,要执行函数则需要调用函数名,但匿名函数没有函数名(匿名函数最大的好处就是以后定义函数不用再纠结给函数命什么名了),因而突然间拿到一个lambda表达式,我们会突然间有点迷惑,不知道该怎么样执行它。

  下面代码则给了几个调用并执行匿名函数的方式:

#include <iostream>
#include <vector>
#include <string>
#include <functional> /** function */
#include <algorithm> /** for_each */ /** 建立一个类似创建线程函数的用法的函数来完成执行匿名函数 */
int my_eval(std::function <int(int, int)> f, int x, int y) {
return f(x, y);
} int main() {
/**用法1: 通过将匿名函数存储在变量中,并作为参数传递*/
std::function<int(int, int)> f0 = [](int x, int y) -> int {return x + y; };//lambda 表达式
auto f1 = [](int x, int y) -> int {return x * y; };//没有使用任何外部变量
std::cout << "f0: " << my_eval(f0, 3, 4) << std::endl;
std::cout << "f1: " << my_eval(f1, 2, 3) << std::endl; /**用法2: 保存到vector中*/
std::vector<decltype(f0)> func_s{ f0, f1 };
func_s.push_back([](int x, int y) {return x - y; });
for (auto f : func_s)//遍历匿名函数
std::cout << "f: " << f(3, 1) << std::endl; /**用法3: 使用for_each传入匿名函数*/
std::vector<int> nums{ 1,2,3,4,5 };
int sum = 0; //代码中使用了sum 因而通过[&]引用隐式捕获,如果代码没有使用任何外部变量,则不传递参数
std::for_each(begin(nums), end(nums), [&](int x) {sum += x; });
//x为遍历nums传递的值
std::cout << "f3: " << sum << std::endl; //同上用法 但匿名函数参数有不同的地方
std::string str{ "hello world" };
std::string s = "";
std::string c = " "; //指明外部变量s通过引用捕获,c通过传递值捕获
std::for_each(begin(str), end(str), [&s, c](char x) {s += (x + c); }); std::cout << "f4: " << s << std::endl; /**用法4: 保存到函数指针*/
auto my_lambda_func = [](int x) {std::cout << "f5: " << x << std::endl;};
void(*func_ptr)(int) = my_lambda_func;//注意变量名不能与函数指针名冲突
func_ptr(4);//回调
/**或者直接这样写:
* void(*func_ptr)(int) = [](int x) {std::cout << "f5: " << x << std::endl;};
* func_ptr(4);
*/ /**用法5: 直接存储在auto类型变量中,然后调用*/
auto myfunc = [](int x) {std::cout << "f6: " << x << std::endl;};
myfunc(5); return 0;
}

  运行结果:

f0: 7
f1: 6
f: 4
f: 3
f: 2
f3: 15
f4: h e l l o w o r l d
f5: 4
f6: 5

------------------update 2018-02-15 02:20:50---------------

  前面测试了几个用法,但却不够细节,今天再来补充一些细节。

  实际上,代码中有几个值得探究的地方:

//重新举两个例子:
auto func = [](std::string params) {return params;};
auto func = [](std::string params) -> std::string {return params;};

  上面两个lambda表达式不同的地方就是一个有写明返回类型,一个没有。

  而这两种写法都可以正确进行,但前提是必须使用auto类型让编译器自己判断返回类型,否则就会报错:

  使用正确类型变量:

#include <iostream>
#include <string> int main()
{
std::string func = [](std::string params) {return params;};
std::cout << func("hello world!"); return 0;
}

  报错:

error: conversion from 'main()::<lambda(std::__cxx11::string)>' to non-scalar type 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' requested|

  使用错误类型变量:

#include <iostream>
#include <string> int main()
{
int func = [](std::string params) {return params;};
std::cout << func("hello world!"); return 0;
}

  报错:

main.cpp||In function 'int main()':|
main.cpp|6|error: invalid user-defined conversion from 'main()::<lambda(std::__cxx11::string)>' to 'int' [-fpermissive]|
main.cpp|6|note: candidate is: main()::<lambda(std::__cxx11::string)>::operator std::__cxx11::string (*)(std::__cxx11::string)() const <near match>|
main.cpp|6|note: no known conversion from 'std::__cxx11::string (*)(std::__cxx11::string) {aka std::__cxx11::basic_string<char> (*)(std::__cxx11::basic_string<char>)}' to 'int'|
main.cpp|7|error: 'func' cannot be used as a function|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

  那如果这样呢:

#include <iostream>
#include <string> int main()
{
int func = [](std::string params) -> int {return params;};
std::cout << func("hello world!"); return 0;
}

  报错:

main.cpp|6|error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'int' in return|
main.cpp||In function 'int main()':|
main.cpp|6|error: invalid user-defined conversion from 'main()::<lambda(std::__cxx11::string)>' to 'int' [-fpermissive]|
main.cpp|6|note: candidate is: main()::<lambda(std::__cxx11::string)>::operator int (*)(std::__cxx11::string)() const <near match>|
main.cpp|6|note: no known conversion from 'int (*)(std::__cxx11::string) {aka int (*)(std::__cxx11::basic_string<char>)}' to 'int'|
main.cpp|7|error: 'func' cannot be used as a function|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

  这样呢:

#include <iostream>
#include <string> int main()
{
std::string func = [](std::string params) -> std::string {return params;};
std::cout << func("hello world!"); return 0;
}

  然后会发现竟然也报错了:

main.cpp||In function 'int main()':|
main.cpp|6|error: conversion from 'main()::<lambda(std::__cxx11::string)>' to non-scalar type 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' requested|
main.cpp|7|error: no match for call to '(std::__cxx11::string {aka std::__cxx11::basic_string<char>}) (const char [13])'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

  说是从<lambda(std::__cxx11::string)>转换为非标量类型??不太明白。

  然后试试这样:

#include <iostream>
#include <string> int main()
{
char* func = [](char* params) -> char* {return params;};
std::cout << func("hello world!"); return 0;
}

  报错:

main.cpp||In function 'int main()':|
main.cpp|7|error: cannot convert 'main()::<lambda(char*)>' to 'char*' in initialization|
main.cpp|8|error: 'func' cannot be used as a function|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

  就是说不能在初始化过程中对lambda进行转换。

  然后再试试这样:

#include <iostream>
#include <string> int main()
{
std::string (*func)(std::string) = [](std::string params) {return params;};
std::cout << func("hello world!"); return 0;
}

  这个代码能正常显示结果,但最开始运行了几次会在显示结果后崩溃...不知道为什么,然后后面运行就正常了,但显示完结果后会感觉很明显的停顿一下才显示Press any key to continue.

  这样写就能正常显示,且没有停顿:

#include <iostream>
#include <string> int main()
{
void(*func)(std::string) = [](std::string params) {std::cout << params;};
func("hello world!"); return 0;
}

  emmm...

  从这几个测试可以看出,貌似编译器处理lambda表达式的时候,不能直接将匿名函数赋值给一个具有基本类型的变量,这个过程原理是什么还不太明白。但可以直接赋给一个函数指针,然后调用。

  但需要注意:函数指针必须带有类型,不能用auto,因为程序无法推断回调函数的类型。比如:

#include <iostream>
#include <string> int main()
{
auto(*func)(std::string) = [](std::string params) {std::cout << params;};
func("hello world!"); return 0;
}

  所以,可见使用auto自动判断类型是一个很简便的写法,但也需要正确使用:

#include <iostream>
#include <string> int main()
{
auto func = [](std::string params) {std::cout << params;};
func("hello world!"); return 0;
}

  总结一下:

  匿名函数可以不用写返回的类型也就是:

 -> return_type

  完全可以省略掉。然后可以存储在正确类型的函数指针或auto类型的变量中进行调用。

--------------------------------------

  参考资料:

    维基百科-C++ 11

C++11 — lambda表达式(匿名函数)的更多相关文章

  1. [二] java8 函数式接口详解 函数接口详解 lambda表达式 匿名函数 方法引用使用含义 函数式接口实例 如何定义函数式接口

    函数式接口详细定义 package java.lang; import java.lang.annotation.*; /** * An informative annotation type use ...

  2. java8函数式接口详解、函数接口详解、lambda表达式匿名函数、方法引用使用含义、函数式接口实例、如何定义函数式接口

    函数式接口详细定义 函数式接口只有一个抽象方法 由于default方法有一个实现,所以他们不是抽象的. 如果一个接口定义了一个抽象方法,而他恰好覆盖了Object的public方法,仍旧不算做接口的抽 ...

  3. Python:lambda表达式(匿名函数)

    lambda表达式: 通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就是指匿名函数. 当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便. 在Python中 ...

  4. lambda表达式 匿名函数

    lambda函数是一种快速定义单行最小函数的方法,是从Lisp借鉴而来的,可以用在任何需要函数的地方. 基础 lambda语句中,冒号前是参数,可以有多个,用逗号分割:冒号右边是返回值. lambda ...

  5. lambda表达式匿名函数

    匿名函数是一个“内联”语句或表达式,可在需要委托类型的任何地方使用.可以使用匿名函数来初始化命名委托,或传递命名委托(而不是命名委托类型)作为方法参数. C# 中委托的发展 在 C# 1.0 中,您通 ...

  6. C++中对C的扩展学习新增语法——lambda 表达式(匿名函数)

    1.匿名函数基础语法.调用.保存 1.auto lambda类型 2.函数指针来保存注意点:[]只能为空,不能写东西 3.std::function来保存 2.匿名函数捕捉外部变量(值方式.引用方式) ...

  7. C++11 lambda 表达式

    C++11 新增了很多特性,lambda 表达式是其中之一,如果你想了解的 C++11 完整特性,建议去这里,这里,这里,还有这里看看.本文作为 5 月的最后一篇博客,将介绍 C++11 的 lamb ...

  8. C++11 lambda 表达式解析

    C++11 新增了很多特性,lambda 表达式是其中之一,如果你想了解的 C++11 完整特性,建议去这里,这里,这里,还有这里看看.本文作为 5 月的最后一篇博客,将介绍 C++11 的 lamb ...

  9. 【C++】C++中的lambda表达式和函数对象

    目录结构: contents structure [-] lambda表达式 lambda c++14新特性 lambda捕捉表达式 泛型lambda表达式 函数对象 函数适配器 绑定器(binder ...

  10. 详解 C++11 lambda表达式

    详解 C++11 lambda表达式   lambda表达式是函数式编程的基础.咱对于函数式编程也没有足够的理解,因此这里不敢胡言乱语,有兴趣的可以自己查找相关资料看下.这里只是介绍C++11中的la ...

随机推荐

  1. 一文搞懂vim复制粘贴

    转载自本人独立博客https://liushiming.cn/2020/01/18/copy-and-paste-in-vim/ 概述 复制粘贴是文本编辑最常用的功能,但是在vim中复制粘贴还是有点麻 ...

  2. 安卓基础(AndroidViewModel)

    今天学习了AndroidViewModel,但是我根据视频上讲解,根据所讲用例,在添加依赖得时候一直报错,后来我请教大佬,他告诉我说是,因为网络得问题,国外得一些依赖有可能下不下来,所以可以下载阿里云 ...

  3. 全排列dfs

    #include <iostream> #include <vector> using namespace std; vector<int> ans; const ...

  4. robotframe常用的类库、对应的方法和属性

    robotframe常用的类库.对应的方法和属性

  5. Jmeter_正则表达式

    元字符+限定符 元字符: . 任意字符 \d 任意单个数字 [0-9] 0-9 [a-z A-Z] 限定符 + 匹配至少大于1次 ? 匹配0次或者1次 * 匹配0次或者多次 {n}匹配n次 在线正则表 ...

  6. dp(出国简历)

    Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要去申请学校了.要申请国外的任何大学,你都要交纳一定的申请费用,这可是很惊人的.Speakless没有 ...

  7. 关于this和base的区别

    一句话总结:在有冲突得时候,base和this能够进行区分,在没有冲突得时候,是一样得. 基于成员调用 基于构造方法 参考: http://www.cnblogs.com/reommmm/archiv ...

  8. python练习:使用二分法查找求近似平方根,使用二分法查找求近似立方根。

    python练习:使用二分法查找求近似平方根,使用二分法查找求近似立方根. 重难点:原理为一个数的平方根一定在,0到这个数之间,那么就对这之间的数,进行二分遍历.精确度的使用.通过最高值和最低值确定二 ...

  9. 主机与虚拟机连接,主机能ping通虚拟机虚拟机ping不通主机问题

    事件描述: 从物理主机ping虚拟机时,能正常返回信息.反之,从虚机ping物理主机时返回信息:Destination Host unreachable.   解决方法: 首先,是因为默认创建的虚拟机 ...

  10. POJ-1087 A Plug for UNIX (网络流)

    思路 电器数1 ~ 100,附带100种接口,注意题目:You notice that some of the devices use plugs for which there is no rece ...