Lambda expressions (since C++11)

Syntax

 
[ captures ] <tparams>(optional)(c++20) ( params ) specifiers(optional) exception attr -> ret { body } (1)  
 
[ captures ] ( params ) -> ret { body } (2)  
 
[ captures ] ( params ) { body } (3)  
 
[ captures ] { body } (4)  
 

上代码:

 // lambdaprj.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include<algorithm>
#include<vector>
#include<iostream>
#include<functional>
using namespace std; /*
* []()->{};
* [ capture list] ( params list) mutable exception attribute -> ret { body }
*/
int main()
{
auto sayHelloWorld = []() {
cout << "hello world" << endl;
};
sayHelloWorld();
auto add_ = [](int a,int b)->int {
return a + b;
};
cout<< add_(, )<<endl; int i = ;//read only
auto add_with_i = [i](int a, int b)->int {
return a + b + i;
};
cout << add_with_i(,)<<endl;
// int j = ;
auto add_with_j_c = [&j](int a, int b)-> int{
j = ;
return a + b + j;
};
cout << add_with_j_c(, ) << " j:" << j << endl;
// vector<int> arr = { ,,,, };
int total = ;
for_each(begin(arr), end(arr),
[&](int x)->int{
return total += x;
});
cout << "total:" << total << endl;
//
vector<int> sort_arr = { ,-,,,,,, };
cout << "unsort : ";
for_each(begin(sort_arr),end(sort_arr),
[](int x) {
cout << x << " "; });
cout << endl;
sort(begin(sort_arr),end(sort_arr),
[](int a, int b)->bool {
return abs(a) < abs(b); });
cout << "sort : ";
for_each(begin(sort_arr), end(sort_arr),
[](int x) {
cout << x << " "; });
cout << endl;
// auto func = [](function<void()> f) {
f();
};
int x = ;
auto func_add = [&]() {
x++;
};
func(func_add);
cout << "x:" << x << endl;
getchar();
return ;
}

参考学习:

1、https://www.youtube.com/watch?v=uk0Ytomv0wY

2、http://en.cppreference.com/w/cpp/language/lambda

lambda modern C++的更多相关文章

  1. Functional Programming without Lambda - Part 1 Functional Composition

    Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...

  2. [C++11] Effective Modern C++ 读书笔记

    本文记录了我读Effective Modern C++时自己的一些理解和心得. item1:模板类型推导 1)reference属性不能通过传值参数传入模板函数.这就意味着如果模板函数需要一个refe ...

  3. Effective Modern C++翻译(1):序言

    /*********************************************************** 关于书: 书是我从网上找到的effective Modern C++的样章,内 ...

  4. 《Effective Modern C++》翻译--简单介绍

    北京时间2016年1月9日10:31:06.正式開始翻译.水平有限,各位看官若有觉得不妥之处,请批评指正. 之前已经有人翻译了前几个条目,有些借鉴出处:http://www.cnblogs.com/m ...

  5. (转)基于 WPF + Modern UI 的 公司OA小助手 开发总结

    原文地址:http://www.cnblogs.com/rainlam163/p/3365181.html 前言: 距离上一篇博客,整整一个月的时间了.人不能懒下来,必须有个阶段性的总结,算是对我这个 ...

  6. 决定干点事儿--翻译一下《effective modern c++》

    写了非常多关于C++11的博客.总是认为不踏实,非常多东西都是东拼西凑.市场上也非常少有C++11的优秀书籍,但幸运的是Meyers老爷子并没有闲赋.为我们带来了<effective moder ...

  7. 基于 WPF + Modern UI 的 公司OA小助手 开发总结

    前言: 距离上一篇博客,整整一个月的时间了.人不能懒下来,必须有个阶段性的总结,算是对我这个阶段的一个反思.人只有在总结的过程中才会发现自己的不足. 公司每天都要在OA系统上上班点击签到,下班点击签退 ...

  8. C++ lambda的演化

    翻译自https://www.bfilipek.com/2019/02/lambdas-story-part1.html与https://www.bfilipek.com/2019/02/lambda ...

  9. Effective Modern C++:01类型推导

    C++的官方钦定版本,都是以ISO标准被接受的年份命名,分别是C++98,C++03,C++11,C++14,C++17,C++20等.C++11及其后续版本统称为Modern C++. C++11之 ...

随机推荐

  1. Appium 之处理首次启动手机App时的系统权限弹框

    一般首次启动一个手机App时都会有系统权限弹框,如下图所示: 权限弹窗上面的按钮都是固定的,只需要定位到“ALLOW”按钮,点击就可以了,代码如下: 这里主要用selenium里面的显示等待模块(We ...

  2. JAVA AQS源码分析

    转自:  http://www.cnblogs.com/pfan8/p/5010526.html JAVA AQS的全称为(AbstractQueuedSynchronizer),用于JAVA多线程的 ...

  3. [转]Clean up after Visual Studio

    本文转自:https://weblogs.asp.net/psheriff/clean-up-after-visual-studio As programmer’s we know that if w ...

  4. day15(生成器send方法,递归,匿名函数,内置函数)

    一,复习 ''' 1.带参装饰器 - 自定义 | wraps def wrap(info) def outer1(func): from functools import wraps @wraps(f ...

  5. Linux服务器使用XShell上传下载文件

    在学习Linux过程中,我们常常需要将本地文件上传到Linux主机上,这里简单记录下使用Xsheel工具进行文件传输 1:首先连接上一台Linux主机 2:输入rz命令,看是否已经安装了lrzsz,如 ...

  6. Maven Compilation error [package org.testng.annotations does not exist]

    背景 在执行mvn test的时候,提示package org.testng.annotations does not exist 解决办法 Open pom.xml file. Go to &quo ...

  7. bash shell & 环境变量

    root是没有~/.bashrc的,只. /etc/profile即可,/etc/profile和~/.bashrc的作用类似,只是作用域不同,都是写死的export,也有动态的脚本去设置命令和环境变 ...

  8. php中mvc框架总结1(7)

    1.代码结构的划分: 目前的目录结构: /站点根目录 /application/应用程序目录 Model/模型目录 View/视图目录 Back/后台 front/ test/测试平台 Control ...

  9. pkuwc 前的任务计划

    菜鸡 wxw 的计划(肯定会咕咕咕 12.27 luogu P4244 [SHOI2008]仙人掌图 II(咕咕咕 luogu P4246 [SHOI2008]堵塞的交通 (没有咕! luogu P1 ...

  10. hadoop-1.2.1-1.x86_64.rpm、jdk-7u45-linux-x64.tar.gz安装(64位)

    一,   配置信息 机器是64位,所以操作系统.软件都是64位的. 操作系统:CentOS6.2(64位): Hadoop是hadoop-1.2.1-1.x86_64.rpm: JDK是jdk-7u4 ...