2-6 Working with Lambdas
在C++中使用匿名函数,格式如下:[] () {};
Using a Lambda to Print array Values
#include <algorithm>
#include <array>
#include <cstdint>
#include <iostream> int main()
{
using MyArray = std::array<uint32_t, >;
MyArray myArray{ , , , , }; std::for_each(myArray.cbegin(),
myArray.cend(),
[](auto&& number) {
std::cout << number << std::endl;
}); return ;
}
Referencing a Closure in a Variable
#include <algorithm>
#include <array>
#include <cstdint>
#include <iostream>
#include <typeinfo> int main()
{
using MyArray = std::array<uint32_t, >;
MyArray myArray{ , , , , }; auto myClosure = [](auto&& number) {
std::cout << number << std::endl;
};
std::cout << typeid(myClosure).name() << std::endl; std::for_each(myArray.begin(),
myArray.end(),
myClosure); return ;
}

下面是一些关于闭包的使用:
1.Passing a Closure into a Function
#include <algorithm>
#include <array>
#include <cstdint>
#include <functional>
#include <iostream>
#include <typeinfo> using MyArray = std::array<uint32_t, >; void PrintArray(const std::function<void(MyArray::value_type)>& myFunction)
{
MyArray myArray{ , , , , }; std::for_each(myArray.begin(),
myArray.end(),
myFunction);
} int main()
{
auto myClosure = [](auto&& number) {
std::cout << number << std::endl;
};
std::cout << typeid(myClosure).name() << std::endl;
PrintArray(myClosure);
return ;
}
2.Copy an array into a vector through a lambda using the capture block
#include <algorithm>
#include <array>
#include <cstdint>
#include <functional>
#include <iostream>
#include <typeinfo>
#include <vector> using MyArray = std::array<uint32_t, >;
using MyVector = std::vector<MyArray::value_type>; void PrintArray(const std::function<void(MyArray::value_type)>& myFunction)
{
MyArray myArray{ , , , , }; std::for_each(myArray.begin(),
myArray.end(),
myFunction);
} int main()
{
MyVector myCopy;
auto myClosure = [&myCopy](auto&& number) {
std::cout << number << std::endl;
myCopy.push_back(number);
};
std::cout << typeid(myClosure).name() << std::endl; PrintArray(myClosure); std::cout << std::endl << "My Copy: " << std::endl;
std::for_each(myCopy.cbegin(),
myCopy.cend(),
[](auto&& number){
std::cout << number << std::endl;
}); return ;
}
3.C++11 Compatible Lambda Function
#include <algorithm>
#include <array>
#include <cstdint>
#include <functional>
#include <iostream>
#include <typeinfo>
#include <vector> using MyArray = std::array<uint32_t, >;
using MyVector = std::vector<MyArray::value_type>; void PrintArray(const std::function<void(MyArray::value_type)>& myFunction)
{
MyArray myArray{ , , , , }; std::for_each(myArray.begin(),
myArray.end(),
myFunction);
} int main()
{
MyVector myCopy;
auto myClosure = [&myCopy](const MyArray::value_type&number) {
std::cout << number << std::endl;
myCopy.push_back(number);
};
std::cout << typeid(myClosure).name() << std::endl; PrintArray(myClosure); std::cout << std::endl << "My Copy: " << std::endl;
std::for_each(myCopy.cbegin(),
myCopy.cend(),
[](const MyVector::value_type&number){
std::cout << number << std::endl;
}); return ;
}
4.keyward mutable
2-6 Working with Lambdas的更多相关文章
- Lambdas in Java 8--reference
Part 1 reference:http://jaxenter.com/lambdas-in-java-8-part-1-49700.html Get to know lambda expressi ...
- Java 8: Lambdas和新的集合Stream API
Lambda是Java8的主要特色,Java 8: Lambdas & Java Collections | zeroturnaround.com一文介绍了使用Lambda集合处理大量数据的方 ...
- 用 JMH 检测 Lambdas 序列化性能
本文将介绍如何进行 Java Lambdas 序列化性能检测.Lambdas 的重要性以及 Lambdas 在分布式系统中的应用. Lambdas 表达式是 Java 8 中万众期待的新特性,其若干用 ...
- From delegates to lambdas z
I thought of naming this post “Evolution of lambdas in C#”, then I decided that wouldn’t be closest ...
- lambdas了解
Lambdas了解 功能接口的一个极其宝贵的特性是可以使用lambdas实例化它们.以下是一些关于lambdas的例子: 以逗号分隔的输入列表,左边是指定类型的输入,右边是返回的块: ...
- C++11 带来的新特性 (4)—— 匿名函数(Lambdas)
1 语法 Lambdas并不是新概念,在其它语言中已经烂大街了.直接进入主题,先看语法: [ captures ] ( params ) specifiers exception attr -> ...
- Java 8 新特性——Lambdas 表达式
本文内容 引入 测试数据 collect(toList()) map filter flatMap max 和 min reduce 整合操作 参考资料 Java 8 对核心类库的改进主要包括集合类的 ...
- Swift 中的闭包与 C 和 Objective-C中的 blocks 以及其它一些编程语言中的 lambdas 比較类似。
闭包是功能性自包括模块,能够在代码中被传递和使用. Swift 中的闭包与 C 和 Objective-C中的 blocks 以及其它一些编程语言中的 lambdas 比較相似. 闭包能够 捕获 和 ...
- RxJava 设计理念 观察者模式 Observable lambdas MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- lambdas vs. method groups
Update: Due to a glitch in my code I miscalculated the difference. It has been updated. See full his ...
随机推荐
- C++读入XML文件
最近要做一个VRP的算法,测试集都是放在Xml文件中,而我的算法使用C++来写,所以需要用C++来读取Xml文件. 在百度上搜"C++读取Xml文件",可以出来很多博客,大多数是关 ...
- Composer实现PHP中类的自动加载
本篇博客承接上一篇,学习一下Composer实现的PHP的类的自动加载方式.首先说明一下,Composer是PHP针对PHP语言的第三方的依赖管理工具,将工程所用到的依赖文件包含在composer.j ...
- ue4 c++ 接口
使用UE4接口比起普通的高级语言,要多做很多工作,是因为要兼容蓝图的使用,有一些小坑需要注意,开始吧. 1.新建接口类 打开UE4编辑器,与往常一样,新建C++类,然后选择Object继承,然后取名字 ...
- android常用调试工具fiddle、wireshark和android studio的配置
Fiddle配置android代理 在wifi的同一个局域网环境的windows主机中安装fiddler,并且启动,如本次192.168.3.14 在android手机端配置代理为该主机 还有一种方式 ...
- 调用WCF Client客户端测试
之后
- solr清空全部索引
http://blog.csdn.net/qing419925094/article/details/42142117
- ActiveX: 如何用.inf和.ocx文件生成cab文件
ActiveX: 如何用.inf和.ocx文件生成cab文件
- TCP协议疑难杂症全景解析
说明: 1).本文以TCP的发展历程解析容易引起混淆,误会的方方面面2).本文不会贴大量的源码,大多数是以文字形式描述,我相信文字看起来是要比代码更轻松的3).针对对象:对TCP已经有了全面了解的人. ...
- @MappedSuperclass注解的使用说明
转载自:http://blog.sina.com.cn/s/blog_7085382f0100uk4p.html 基于代码复用和模型分离的思想,在项目开发中使用JPA的@MappedSuperclas ...
- Winform 中DataGridView控件添加行标题
有很多种方法. 1.可以在DataGridView控件中的RowStateChanged事件改变行标题单元格的值(Row.HeaderCell.Value) /// <summary> / ...