美文网首页
C++ Lambda简明教程

C++ Lambda简明教程

作者: 煎饼_Jabin | 来源:发表于2020-10-25 14:26 被阅读0次
  • C++ Primer Plus 中文 第六版
  • Essential C++ 中文版
  • 深度探索 C++ 对象模型
  • C++ 程序设计语言(1-3)(4) 第四版 英文版
  • A Tour of C++ (Second Edition)
  • Professional C++ (4th Edition)

Lambda

Expressions 表达式

[capture](parameters) -> returnType{
 statements;
}

Capture 关键字

When a lambda definition is executed, for each variable that the lambda captures, a clone of that variable is made (with an identical name) inside the lambda. These cloned variables are initialized from the outer scope variables of the same name at this point.

The captured variables of a lambda are clones of the outer scope variables, not the actual variables

Capture by reference
Capture by value
Capture by both (mixed capture)
int health{ 33 };
int armor{ 100 };
std::vector<CEnemy> enemies{};

[](){}; //default 

//capture health by value 
[health](){}

//can update health value with mutable
[health]() mutable{}

// Capture health and armor by value, and enemies by reference.
[health, armor, &enemies](){};
 
// Capture enemies by reference and everything else by value.
[=, &enemies](){};
 
// Capture armor by value and everything else by reference.
[&, armor](){};
 
// Illegal, we already said we want to capture everything by reference.
[&, &armor](){};
 
// Illegal, we already said we want to capture everything by value.
[=, armor](){};
 
// Illegal, armor appears twice.
[armor, &health, &armor](){};
 
// Illegal, the default capture has to be the first element in the capture group.
[armor, &](){};

Examples 例子

std::string label = "nut";
std::array<std::string, 4> arr{"apple", "banana", "walnut", "lemon"};

// std::find_if takes a pointer to a function
const auto found{std::find_if(arr.begin(), arr.end(), [&label](std::string str) {
     label = "apple";
     return (str.find(label) != std::string::npos);
 })};
auto print = [](auto value) {
        static int count{0};
        std::cout << "count: " << count++ << "value: " << value << std::endl;
    };

print("a");
print("b");
print(1);
print(2);

//we define a lambda and then call it with two different parameters 
//(a string literal parameter, and an integer parameter). This generates 
//two different versions of the lambda (one with a string literal parameter, 
//and one with an integer parameter).

//Most of the time, this is inconsequential. However, note that if the generic 
//lambda uses static duration variables, those variables are not shared between the 
//generated lambdas. 

links:
https://www.learncpp.com/cpp-tutorial/introduction-to-lambdas-anonymous-functions/ https://www.learncpp.com/cpp-tutorial/lambda-captures/

相关文章

  • C++ Lambda简明教程

    C++ Primer Plus 中文 第六版Essential C++ 中文版深度探索 C++ 对象模型C++ 程...

  • C++多任务编程简明教程 (1) - C++的多任务其实很简单

    C++多任务编程简明教程 (1) - C++的多任务其实很简单 用库的方式无法实现彻底的线程安全!我们需要C++1...

  • C++简明教程

    本教程旨在提取最精炼、实用的C++知识点,供读者快速学习及本人查阅复习所用,后期会持续更新。 基本语法 C++ 语...

  • OPENGL ES 教程

    Android OpenGL ES 简明开发教程一:概述Android OpenGL ES 简明开发教程二:构造O...

  • C++简明教程(一)

    本文内容来自菜鸟教程, C++教程,该篇内容仅作为笔记使用 静态类型编程语言 编译时执行类型检查,而不是运行时执行...

  • C++简明教程(二)

    本文内容来自菜鸟教程, C++教程,该篇内容仅作为笔记使用 C++引用 引用变量是一个别名,也就是说,它是某个已存...

  • C++简明教程(三)

    本文内容来自菜鸟教程, C++教程,该篇内容仅作为笔记使用 继承 基类 & 派生类 一个类可以派生自多个类,这意味...

  • webpack 几个很棒的教程

    初学webpack,网上找到了几个简明易懂的入门教程十分感谢教程的作者! WebPack 简明学习教程 Webpa...

  • Linux 常用命令总结

    linux 命令速查手册linux 常用操作命令 sed 简明教程 awk 简明教程 常用命令 ls ...

  • Socket使用简明教程- AsyncSocket

    Socket使用简明教程- AsyncSocket

网友评论

      本文标题:C++ Lambda简明教程

      本文链接:https://www.haomeiwen.com/subject/ujshnctx.html