2019独角兽企业重金招聘Python工程师标准>>>

The problem

C++ includes useful generic functions like std::for_each and std::transform, which can be very handy. Unfortunately they can also be quite cumbersome to use, particularly if the functor you would like to apply is unique to the particular function.

#include < algorithm > #include < vector > namespace { struct f { void operator ()( int ) { // do something } }; } void func(std::vector < int >& v) { f f; std::for_each(v.begin(), v.end(), f); }

If you only use f once and in that specific place it seems overkill to be writing a whole class just to do something trivial and one off.

In C++03 you might be tempted to write something like the following, to keep the functor local:

void func2(std::vector < int >& v) { struct { void operator ()( int ) { // do something } } f; std::for_each(v.begin(), v.end(), f); }

however this is not allowed, f cannot be passed to a template function in C++03.

The new solution

C++11 introduces lambdas allow you to write an inline, anonymous functor to replace the struct f. For small simple examples this can be cleaner to read (it keeps everything in one place) and potentially simpler to maintain, for example in the simplest form:

void func3(std::vector < int >& v) { std::for_each(v.begin(), v.end(), []( int ) { /* do something here */ }); }

Lambda functions are just syntactic sugar for anonymous functors.

Return types

In simple cases the return type of the lambda is deduced for you, e.g.:

void func4(std::vector < double >& v) { std::transform(v.begin(), v.end(), v.begin(), []( double d) { return d < 0.00001 ? 0 : d; } ); }

however when you start to write more complex lambdas you will quickly encounter cases where the return type cannot be deduced by the compiler, e.g.:

void func4(std::vector < double >& v) { std::transform(v.begin(), v.end(), v.begin(), []( double d) { if (d < 0.0001 ) { return 0 ; } else { return d; } }); }

To resolve this you are allowed to explicitly specify a return type for a lambda function, using -> T:

void func4(std::vector < double >& v) { std::transform(v.begin(), v.end(), v.begin(), []( double d) -> double { if (d < 0.0001 ) { return 0 ; } else { return d; } }); }

"Capturing" variables

So far we've not used anything other than what was passed to the lambda within it, but we can also use other variables, within the lambda. If you want to access other variables you can use the capture clause (the [] of the expression), which has so far been unused in these examples, e.g.:

void func5(std::vector < double >& v, const double & epsilon) { std::transform(v.begin(), v.end(), v.begin(), [epsilon]( double d) -> double { if (d < epsilon) { return 0 ; } else { return d; } }); }

You can capture by both reference and value, which you can specify using = and &:

  • [&epsilon] capture by reference
  • [&, epsilon] specify that the default way of capturing is by reference and what we want to capture
  • [=, &epsilon] capture by value by default, but for epsilon use reference instead

The generated operator() is const by default, with the implication that captures will be const when you access them by default. This has the effect that each call with the same input would produce the same result, however you can mark the lambda as mutable to request that the operator() that is produced is not const.

转载于:https://my.oschina.net/jacobin/blog/193736

What is a lambda expression in C++11?相关推荐

  1. Lambda Expression

    "Lambda 表达式"(lambda expression)是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的lambda抽象(lambda abstra ...

  2. java lambda函数_Java SE 8新功能介绍:使用Lambda Expression进行函数式编程

    java lambda函数 " Java SE 8新功能浏览 "系列的这篇文章将深入了解Lambda表达式 . 我将向您展示Lambda表达式的几种不同用法. 它们都具有功能接口的 ...

  3. Java SE 8新特性导览:使用Lambda Expression进行函数式编程

    " Java SE 8新功能浏览 "系列的这篇文章将深入了解Lambda表达式 . 我将向您展示Lambda表达式的几种不同用法. 它们都具有功能接口的共同实现. 我将解释编译器如 ...

  4. [原创]深入理解C# 3.x的新特性(3):从Delegate、Anonymous Method到Lambda Expression

    较之前一个版本,对于C# 3.x和VB 9来说,LINQ是最具吸引力的.基本上很多的新的特性都是围绕着LINQ的实现来设计的.借助Extension Method,我们可以为LINQ定义一系列的Ope ...

  5. C#体贴之处点滴 - extention method, lambda expression, anonymous method

    说的是C#如何体贴程序员,而非.NET Framework. 这是C#对Delegate相关领域下的精微功夫:     List<Product> products = Product.G ...

  6. IDEA:Lambda expression are not supported at language level ‘5‘

    错误 Lambda expression are not supported at language level '5' Error:(28, 39) java: -source 1.5 中不支持 l ...

  7. 0x55——C#中的Lambda Expression

    本文主要参考MSDN上的Lambda Expression(C# Programming Guide),博文Lambda Expression和code project上的博文Exploring La ...

  8. Lambda expression are not supported at language level '5'

    错误信息: Lambda expression are not supported at language level '5' Error:(28, 39) java: -source 1.5 中不支 ...

  9. java expression 用法_浅析Java 8新特性Lambda Expression

    什么是Lambda Expression 对于Lambda Expression,我的理解是,它是一个函数表达式,如下: (int x, int y) -> x - y 符号左边定义了函数的输入 ...

最新文章

  1. Java项目:网上电子书城项目(java+SSM+JSP+maven+Mysql)
  2. 推荐C、C++、Java、网络安全、Unix、Linux 一些编程书
  3. 《研磨设计模式》chap22 装饰模式Decorator(4)AOP+总结
  4. 《剑指offer》第四题(二维数组中的查找)
  5. 攻防世界-web-ics-04-从0到1的解题历程writeup
  6. SQL Server 的通用分页显示存储过程
  7. 一个学者科研的感受,推荐一下(小木虫)
  8. android界面初始化设计,界面数据初始化及各个按钮功能的实现
  9. 天津计算机专业排名2015,2015年南开大学计算机类专业最低分是多少?
  10. 低代码开发是如何解决企业招聘技术人才难题?
  11. PHP条件语句总结,php条件语句的总结
  12. 最简单的基于DirectShow的示例:视频播放器图形界面版
  13. MSAgent技术应用
  14. 矩阵转置相关公式_线性代数入门——矩阵的转置运算及对称矩阵的概念
  15. VC 2008下安装与配置OpenCV2.1
  16. 小鲸云隔空充电设备如何解决你的手机充电烦恼?
  17. 深入理解oracle的context,读者对于《深入解析Oracle》的评价
  18. 万物互联之边缘计算岗位分析
  19. 浅谈JSP的发展历史
  20. 网络异常无法连接远程服务器,《Chess Rush》网络异常进不去怎么回事 无法连接服务器解决方法...

热门文章

  1. LeetCode 613. Shortest Distance in a Line --SQL
  2. 贪吃蛇程序 php,php,函数 Web程序 - 贪吃蛇学院-专业IT技术平台
  3. python如何跨模块调用变量_Python跨模块用户定义的全局变量:在其他模块运行时调用它们的问题...
  4. 地图不显示_地图不显示脚步,枪声没有方向标记,职业比赛有何不同?
  5. yum安装nginx
  6. IOS后台运行机制详解(二)
  7. 包package,权限修饰符
  8. 开发工具之Android Studio快捷键
  9. 零基础学习python爬虫_教你零基础如何入门Python爬虫!
  10. 计算机二年级考试word,计算机二年级MSOffice真题.docx