在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的更多相关文章

  1. Lambdas in Java 8--reference

    Part 1 reference:http://jaxenter.com/lambdas-in-java-8-part-1-49700.html Get to know lambda expressi ...

  2. Java 8: Lambdas和新的集合Stream API

    Lambda是Java8的主要特色,Java 8: Lambdas & Java Collections | zeroturnaround.com一文介绍了使用Lambda集合处理大量数据的方 ...

  3. 用 JMH 检测 Lambdas 序列化性能

    本文将介绍如何进行 Java Lambdas 序列化性能检测.Lambdas 的重要性以及 Lambdas 在分布式系统中的应用. Lambdas 表达式是 Java 8 中万众期待的新特性,其若干用 ...

  4. From delegates to lambdas z

    I thought of naming this post “Evolution of lambdas in C#”, then I decided that wouldn’t be closest ...

  5. lambdas了解

    Lambdas了解 功能接口的一个极其宝贵的特性是可以使用lambdas实例化它们.以下是一些关于lambdas的例子: 以逗号分隔的输入列表,左边是指定类型的输入,右边是返回的块:          ...

  6. C++11 带来的新特性 (4)—— 匿名函数(Lambdas)

    1 语法 Lambdas并不是新概念,在其它语言中已经烂大街了.直接进入主题,先看语法: [ captures ] ( params ) specifiers exception attr -> ...

  7. Java 8 新特性——Lambdas 表达式

    本文内容 引入 测试数据 collect(toList()) map filter flatMap max 和 min reduce 整合操作 参考资料 Java 8 对核心类库的改进主要包括集合类的 ...

  8. Swift 中的闭包与 C 和 Objective-C中的 blocks 以及其它一些编程语言中的 lambdas 比較类似。

    闭包是功能性自包括模块,能够在代码中被传递和使用. Swift 中的闭包与 C 和 Objective-C中的 blocks 以及其它一些编程语言中的 lambdas 比較相似.  闭包能够 捕获 和 ...

  9. RxJava 设计理念 观察者模式 Observable lambdas MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  10. lambdas vs. method groups

    Update: Due to a glitch in my code I miscalculated the difference. It has been updated. See full his ...

随机推荐

  1. CSS 单行溢出文本显示省略号...的方法(兼容IE FF)(转)

    http://www.52css.com/article.asp?id=602 ===================================================     html ...

  2. java二

    一,面向对象 面向对象,似乎是太抽象了点,没人敢拍着胸脯说我面向对象学到了100%,纵然如此,了解面向对象的思想对于学好java等面向对象编程语言有着莫大的好处,因为一通百通,同样是面向对象,等你精通 ...

  3. EBS中OPM成本更新处理流程及对应的表结构、SLA表

    OPM成本更新流程:  1.跑实际成本处理  功能作用:计算成本  2.成本更新        功能作用:更新成本  3.OPM会计预处理程序->活动->提交流程 功能作用:是创建会计事件 ...

  4. 用CMake设置Visual Studio工程中第三方库

    较大的工程文件中一般会调用动态库或者静态库,如果这些库文件是当前工程包含的项目,CMake会自动识别并添加Debug和Release编译时需要的库文件路径和文件名,可以使用命令: Target_Lin ...

  5. Build OpenCV text(OCR) module on windows

    Background. AOI software needs to use the OCR feature to recognize the texts on the chips. Because o ...

  6. JCL笔记

    本文转自<http://leowzy.iteye.com/blog/888931> ---------------------------------------------------- ...

  7. 解决zookeeper报错[NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@362] - Exception causing close

    zookeeper.out报错: 2016-12-10 18:05:46,958 [myid:3] - WARN  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181 ...

  8. 第三周作业--VS 2013 单元测试

    VSTS可以实现自动测试,简而言之就是编写一个测试程序的程序,通过输入自动判断输出是否与预想相符,并给出代码覆盖率等等一大堆统计数据,以下为如何利用这一功能的简介. 1.新建项目,创建一个类.即要完成 ...

  9. Office 365系列六 ------ 创建sharepoint online网站

    这节跟大家介绍简单的创建sharep online私有网站集,sharepoint online 可以给我们提供开箱即用的功能,比如文档库:可以给我们取代File Server,提供了版本管理,版本变 ...

  10. 平板上的js和电脑上js的不同之处

    一.事件 1.平板上没有:onmousedown,onmouseup,onmousemove等事件,由ontouchstart,ontouchmove,ontounchend替代 2.位置问题:平板上 ...