在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. golang在linux下的开发环境部署[未完]

    uname -a Linux symons_laptop 4.8.2-1-ARCH #1 SMP PREEMPT Mon Oct 17 08:11:46 CEST 2016 x86_64 GNU/Li ...

  2. 游戏笔记之《Fez》

    FEZ 游戏名称:菲斯 英文名称:Fez 游戏类型:ACT(动作游戏),AVG(冒险游戏) 游戏制作:Polytron Corporation 游戏发行:Trapdoor 游戏平台:PC 游戏画面: ...

  3. 关于Java static 的学习心得

    static,大家都很熟悉.但是要说真的懂,那就很少了.(当然我也不是很懂,但不妨碍学习吗.) 首先,我认为static修饰的成员就是属于类本身的成员.如果你加了一个static修饰符,好吧,那就相当 ...

  4. 关于C#的委托(delegate)的自我理解

    首先描述一个事情,一个老师饿了,他要去买东西填饱肚子,然后他发现他的学生“小李”在玩,没学习,于是就委托“小李”去帮他买吃的. 根据这件事我们来分析: 首先得有个老师(老师饿了是他的方法,老师买东西也 ...

  5. AOP和IOC的作用

    IOC:控制反转,是一种设计模式.一层含义是控制权的转移:由传统的在程序中控制依赖转移到由容器来控制:第二层是依赖注入:将相互依赖的对象分离,在spring配置文件中描述他们的依赖关系.他们的依赖关系 ...

  6. 揭秘 typedef四用途与两陷阱[转]

    自 http://niehan.blog.techweb.com.cn/archives/325.html typedef用来声明一个别名,typedef后面的语法,是一个声明.本来笔者以为这里不会产 ...

  7. WebKit的CSS扩展(WebKit是私有属性)

    http://www.css88.com/webkit/-webkit-touch-callout/ -webkit-tap-highlight-color 是一个 不规范的属性(unsupporte ...

  8. 并发编程中.net与java的一些对比

    Java在并发编程中进行使用java.util.concurrent.atomic来处理一些轻量级变量 如AtomicInteger AtomicBoolean等 .Net中则使用Interlocke ...

  9. VC++ 截屏代码,并保存为想要的格式(BMP,JPG,PNG,GIF等格式)

    void CCaptionScreenDlg::Screen(char* filename) { HDC hdcSrc = ::GetDC(NULL); int nBitPerPixel = GetD ...

  10. DSP中的段

    虽然,C语言是一种相对高效的高级语言,并且TI提供的C编译器还结合硬件特点支持三级优化功能,但生成的汇编代码效率仍可能会不尽人意.如作者预使用环型缓冲区管理功能,这就要求该缓冲区应被定位到相对特定的位 ...