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语言PIC32 serial bootloader和C#语言bootloader PC端串口通信程序
了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 今天介绍下我新完成的为 ...
- 第三次作业:caculator
第三次作业 作业链接 ********* 遇到的问题: Scan类: 队列的使用方法不了解,上网查询并自己练习了一下才初步了解,才运用到作业 . 判断数字用的 if (input[i] >= ' ...
- HEX文件格式和其校验算法
这次我将在原来的基础上(http://www.cnblogs.com/libra13179/p/5787084.html)继续讲解HEX文件的格式 打开app_valid_setting_apply. ...
- java自定义异常(Exception、throws、try-catch)
一.What is ... 异常处理就是容错处理机制.通过构造一个陷阱来捕获运行时的可预见错误,经对该错误进行适当处理后,让程序能继续运行不至于崩溃. 二.Who will ... 异常由系统环境引发 ...
- Java线程问题分析定位
Java线程问题分析定位 分析步骤: 1.使用top命令查看系统资源占用情况,发现Java进程占用大量CPU资源,PID为11572: 2.显示进程详细列表命令:ps -mp 11572 -o THR ...
- 一个简单的synchronized多线程问题、梳理与思考
一个程序,多个线程同时操作一个变量,给这个变量+1().功能很简单,可是怎么样去实现呢?这其中涉及到了哪些问题? 最基础想法 见代码: public class Test extends Thread ...
- 考查SQLite 3索引对整数排序的性能影响
做个实验,想了解SQLite3索引对整数排序的性能影响. 用这个测试表,考查绿色那列: id name date 自增型主键 字符串型,随机生成 整数型 随机生成,范围0到54354354 1 bMz ...
- [Altera]PLL仿真
EDA Tools: 1.Quartus II 13.1(64-bit) 2.Modelsim SE-64 10.1c Time: 2016.05.05 ----------------------- ...
- MyEclipse 序列号生成代码
根据程序运行提示输入用户名即可生成注册码 import java.io.*; public class MyEclipseGen { private static final String LL = ...
- diamond专题(一)– 简介和快速使用
(转自 http://blog.csdn.net/zh_winer/article/details/50395024) 一.概况 diamond是淘宝内部使用的一个管理持久配置的系统,它的特点是简单 ...