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 ...
随机推荐
- .NET (三)委托第三讲:内置委托Action
.NET 为我们提供了无返回值的内置委托 Action,代码如下: // 摘要: // 封装一个方法,该方法只有一个参数并且不返回值. // // 参数: // obj: // 此委托封装的方法的参数 ...
- Centos 6.5 下php5.6.2 的编译安装
纯净的Centos 6.5系统 配置163yum源 (稍后会写一篇文档) 安装 开发软件包:yum -y groupinstall "Development Tools" 安 ...
- 微信支付之JSAPI开发第一篇-基本概念
申请权限 具体步骤就不说了,进入微信支付商户平台进行申请认证,认证成功后在微信公众号后台会有个微信支付的菜单按钮,点击后会有如下的信息 配置 在进行微信支付开发之前,必须进行一些必要的配置,如果这些配 ...
- TSP问题——动态规划
Traveling Salesman Problem Description: Time Limit: 4sec Memory Limit:256MB 有编号1到N的N个城市,问从1号城市出发, ...
- 在 node.js 的 express web 框架中自动注册路由
该方法主要是动态注册自己写的 router . 注册器 router 文件名为 loader.js . var express = require('express'); var fs = requ ...
- windows下手动安装和配置xamarin
安装xamarin xamarin官方给出了两种安装方式,自动安装和手动安装. 自动安装比较简单,到http://xamarin.com/download下载xamarininstaller.exe ...
- UIImagePickerController的知识点
1.UIImagePickerController是系统提供的用来获取图片或视频的接口,使用UIImagePickerController类来获取图片的基本步骤如下: a.初始化UIImagePick ...
- Device nodes and device stacks
[Device nodes and device stacks] 链接:https://msdn.microsoft.com/en-us/library/windows/hardware/ff5547 ...
- PHP 7 Xdebug 深深的坑
zend_extension 7.0 下的 php.ini 配置 [XDebug];; Only Zend OR (!) XDebugzend_extension=f:\xampp\php\ext\p ...
- SQL Server 导入数据失败:无法在只读列“Id”中插入数据
解决方案: 在数据库导入"选择源表和源视图"->点击对应表后的“编辑映射”按钮->勾选“启用标识列插入”->导入即可.