lambda modern C++
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 ;
}
参考学习:
lambda modern C++的更多相关文章
- 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 ...
- [C++11] Effective Modern C++ 读书笔记
本文记录了我读Effective Modern C++时自己的一些理解和心得. item1:模板类型推导 1)reference属性不能通过传值参数传入模板函数.这就意味着如果模板函数需要一个refe ...
- Effective Modern C++翻译(1):序言
/*********************************************************** 关于书: 书是我从网上找到的effective Modern C++的样章,内 ...
- 《Effective Modern C++》翻译--简单介绍
北京时间2016年1月9日10:31:06.正式開始翻译.水平有限,各位看官若有觉得不妥之处,请批评指正. 之前已经有人翻译了前几个条目,有些借鉴出处:http://www.cnblogs.com/m ...
- (转)基于 WPF + Modern UI 的 公司OA小助手 开发总结
原文地址:http://www.cnblogs.com/rainlam163/p/3365181.html 前言: 距离上一篇博客,整整一个月的时间了.人不能懒下来,必须有个阶段性的总结,算是对我这个 ...
- 决定干点事儿--翻译一下《effective modern c++》
写了非常多关于C++11的博客.总是认为不踏实,非常多东西都是东拼西凑.市场上也非常少有C++11的优秀书籍,但幸运的是Meyers老爷子并没有闲赋.为我们带来了<effective moder ...
- 基于 WPF + Modern UI 的 公司OA小助手 开发总结
前言: 距离上一篇博客,整整一个月的时间了.人不能懒下来,必须有个阶段性的总结,算是对我这个阶段的一个反思.人只有在总结的过程中才会发现自己的不足. 公司每天都要在OA系统上上班点击签到,下班点击签退 ...
- C++ lambda的演化
翻译自https://www.bfilipek.com/2019/02/lambdas-story-part1.html与https://www.bfilipek.com/2019/02/lambda ...
- Effective Modern C++:01类型推导
C++的官方钦定版本,都是以ISO标准被接受的年份命名,分别是C++98,C++03,C++11,C++14,C++17,C++20等.C++11及其后续版本统称为Modern C++. C++11之 ...
随机推荐
- Centos 7 手把手教你使用YUM方式安装并配置Nginx+php7-fpm+MySQL
需要准备的内容 一台纯净系统的服务器 远程连接服务器的工具 (我这里使用Xshell) 安装nginx 链接上服务器后执行 yum install nginx 这里需要输入y 后回车,开始安装ngi ...
- 项目中使用WCF替换asmx Web service总结
以前项目解决方案中,用http协议的asmx Web service作服务器数据访问入口,在SoapHeader中写入用户名和加盐密码进行身份认证. http asmx服务是明文传输,传输过程中数据很 ...
- 说说eclipse调优,缩短启动时间
初始配置: -startup plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar --launcher.library plug ...
- Linux 非阻塞connect,错误码:EINPROGRESS
当我们以非阻塞的方式来进行连接的时候,返回的结果如果是 -1,这并不代表这次连接发生了错误,如果它的返回结果是 EINPROGRESS,那么就代表连接还在进行中. 后面可以通过poll或者select ...
- Android-SurfaceView生命周期
SurfaceView的生命周期,和 Activity生命周期,Service生命周期,BroadcastReceiver生命周期,等,不一样: 因为SurfaceView显示的是(视频画面,游戏画面 ...
- Android-有序广播
在之前的博客,Android-广播概念,中介绍了(广播和广播接收者)可以组件与组件之间进行通讯,有两种类型的广播(无序广播 和 有序广播),这篇博客就来讲解有序广播的代码实现: 有序广播:接收者 可以 ...
- 在SharePoint列表中使用动态筛选条件[今日][Today]
如果在SharePoint使用了日历控件或者其他列表中有时间字段,用户经常希望能够动态使用条件字段进行筛选,例如希望筛选出开始日期是今天的事件.未来三日的事件. SharePoint的列表筛选条件支持 ...
- Solr相似度算法一:DefaultSimilarity(基于TF-IDF的默认相似度算法)
默认的similarity是基于TF/IDF 模块. 该 similarity有以下配置选项: discount_overlaps –确定是否重叠的标识(标记位置增量为0)都将被忽略在正常计算的时候. ...
- WPF带小箭头的按钮
XAML代码: <ControlTemplate x:Key="btnTpl" TargetType="RadioButton"> <Stac ...
- 使用html2canvas截图并用jspdf打印的问题
之前的方案确实可以打印出a4的大小的pdf,但是也呈现了诸多问题,因为这种方法是截图然后再进行打印的,所以打印出来的效果是模糊的,思前想后决定放弃了这种方式. 最终还是决定使用浏览器自带的打印方法. ...