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. 反混淆 de4dot

    使用SmartAssembly\Dotfuscator等混淆后,反编译应用程序时如何破解? 一款非常NB的反混淆工具:de4dot(开源) Github地址:https://github.com/0x ...

  2. SDNU_ACM_ICPC_2020_Winter_Practice_4th

    H - Triangle   思路:用了斐波那契数列,因为数列中的任意三数都无法组成三角形,所以将1,2,3,,,n变成斐波那契数列就符合条件: #include <iostream> u ...

  3. laradock ppa加速

    环境 laradock-9.7 + win10 + docker desktop laradock 项目地址 问题: 构建 workspace 服务时 卡在这动不了,各种搜资料终于解决了 解决方法: ...

  4. netty(五) channel

    问题 channel 是如何处理发送一半中断后继续重发的 channel 具体作用是什么 概述 这一节我们将介绍 Channel 和内部接口 Unsafe .其中Unsafe 是内部接口,聚合在Cha ...

  5. JVM 初始

    我们刚学习java的时候,从来没有想过new一个对象后,需要我们手动去管理过他的内存空间释放,因为我们知道java有GC垃圾回收器这哥们的存在,他会帮我们处理好一切,这就好比我是皇帝,我想在哪建个行宫 ...

  6. 【PAT甲级】1084 Broken Keyboard (20 分)

    题意: 输入两行字符串,输出第一行有而第二行没有的字符(对大小写不敏感且全部以大写输出). AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #inclu ...

  7. HYStockChart, 股票图(包括K线图、趋势图、成交量、滚动、放大缩小等)

    HYStockChart, 股票图(包括K线图.趋势图.成交量.滚动.放大缩小等) https://www.helplib.com/GitHub/article_127980git 地址 http:/ ...

  8. 正则表达式regex回溯分析

    正则表达式的回溯 现在我们来正式认识一下回溯.以字符串“abbc”为例,正则表达式为“ab{1,3}c”,再匹配的时候,a.b.b,匹配完成,这时候,正则表达式会继续用c和b进行比较,发现不符合,这时 ...

  9. MDC 输出线程信息帮助定位问题

    log4j中的%x ---NDC,%X---MDC 即%x NDC.clear();NDC.push(this.toString());%X{first} %X{last}MDC.put(" ...

  10. 使用scrapy-redis 搭建分布式爬虫环境

    scrapy-redis 简介 scrapy-redis 是 scrapy 框架基于 redis 数据库的组件,用于 scraoy 项目的分布式开发和部署. 有如下特征: 分布式爬取: 你可以启动多个 ...