C++14中lambda表达式新增加的features的使用

news/2025/2/19 18:29:12/

      lambda表达式是在C++11中引入的,它们可以嵌套在其它函数甚至函数调用语句中,C++11中lambda表达式的使用参考:https://blog.csdn.net/fengbingchun/article/details/52653313

      lambda表达式语法如下:除capture和body是必须的,其它均是可选的

[capture] (params) mutable exception attribute -> return-type { body }

      这里介绍下C++14中对lambda表达式新增加的features:

      1.generic lambda:lambda表达式与auto关键字组合,将auto用作参数类型,以下为测试代码:

namespace {//void print_elements(auto& C) // windows need to c++20
template<typename T>
void print_elements(const T& C)
{for (const auto& e : C)std::cout << e << " ";std::cout << "\n";
}}int test_lambda_14_1()
{// reference: https://www.geeksforgeeks.org/generalized-lambda-expressions-c14//* Under the hood, the C++ implementation uses the closure type’s operator() to overload a template functionstruct sum {template<typename T1, typename T2>auto operator()(T1 a, T2 b) const { return (a + b); }};*/auto sum = [](auto a, auto b) {return (a + b);};std::cout << "int sum:" << sum(10, 20) << "\n";std::cout << "float sum:" << sum(1.2f, 2.3f) << "\n";std::cout << "float sum:" << sum(10, 1.5f) << "\n";std::cout << "string sum:" << sum(std::string("csdn addr:"), std::string("https://blog.csdn.net/fengbingchun")) << "\n";auto greater = [](auto a, auto b) -> bool {return (a > b);};std::vector<int> vi = { 1, 4, 2, 6 };std::vector<float> vf = { 4.62f, 161.3f, 62.26f, 13.4f };std::vector<std::string> vs = { "Tom", "Harry", "Ram", "Shyam" };std::sort(vi.begin(), vi.end(), greater);std::sort(vf.begin(), vf.end(), greater);std::sort(vs.begin(), vs.end(), greater);print_elements(vi);print_elements(vf);print_elements(vs);std::vector<std::vector<int>> v = { {7, 8}, {1, 2}, {3, 7}, {4, 5} };std::sort(v.begin(), v.end(), [](std::vector<int>& a, std::vector<int>& b) {return (a[1] < b[1]);});for (int i = 0; i < v.size(); ++i) {for (int j = 0; j < v[0].size(); ++j) {std::cout << v[i][j] << " ";}std::cout << "\n";}return 0;
}

      执行结果如下图所示:

      2.capture initializers:允许创建使用任意表达式初始化captures初始化表达式在创建lambda时计算,而不是在调用时,以下为测试代码:

namespace { int factory(int i) { return i * 10; } }int test_lambda_14_2()
{// reference: https://github.com/AnthonyCalandra/modern-cpp-features#lambda-capture-initializersauto f = [x = factory(2)] { return x; };std::cout << "f:" << f() << "\n";auto generator = [x = 0]() mutable {// this would not compile without 'mutable' as we are modifying x on each callreturn x++;};auto a = generator();auto b = generator();auto c = generator();std::cout << "a:" << a << ",b:" << b << ",c:" << c << "\n";auto p = std::make_unique<int>(1);//auto task1 = [=] { *p = 5; }; // ERROR: std::unique_ptr cannot be copiedauto task2 = [p = std::move(p)] { *p = 5; return *p; }; // OK: p is move-constructed into the closure object// the original p is empty after task2 is createdif (!p)std::cout << "p is empty" << "\n";std::cout << "task2:" << task2() << "\n";auto x = 2;auto f2 = [&r = x, x = x * 10] {++r;return r + x;};std::cout << "f2:" << f2() << ",x:" << x << "\n";return 0;
}

      执行结果如下图所示:

      GitHub:https://github.com/fengbingchun/Messy_Test


http://www.ppmy.cn/news/71619.html

相关文章

Windows免安装MySQL8.0

Windows免安装MySQL8.0 Windows 上的 MySQL 分为安装版和免安装版。 本文介绍如何在 Windows 上配置免安装版的 MySQL 8.0。 1、下载并安装 VC_redist.x64.exe Windows 版的 MySQL 8.0 依赖 Microsoft Visual C 2019 Redistributable Package&#xff08;VC_redist.x64.exe…

io io server disconnect 客户端自动断开怎么办

在socket.io中&#xff0c;如果客户端自动断开连接&#xff0c;有几种常见的情况&#xff0c;例如网络中断或客户端页面被关闭。你可以通过以下方法处理客户端自动断开连接的情况&#xff1a; 在服务器端处理断开连接事件&#xff1a;在服务器端的socket连接处理函数中&#x…

浙江省选模拟题 圈草地

题目描述 在一片 n n n\times n nn的方格地上&#xff0c;种着 n n n块草地&#xff0c;每行每列都有且仅有一块草地。 现在你可以选择两块草地 ( x 1 , y 1 ) (x_1,y_1) (x1​,y1​)和 ( x 2 , y 2 ) (x_2,y_2) (x2​,y2​)&#xff0c;满足 x 1 < x 2 x_1<x_2 x1​&l…

MADDPG-学习笔记(2)

注意&#xff1a;进行本文的实验前&#xff0c;为了加快训练速度&#xff0c;进行了参数调整 num-episodes&#xff1a;由60000改成了10000 lr:由0.01改成了0.1 batch-size:由1024改成了32 1.报错 1.1 AttributeError: Scenario object has no attribute benchmark_data …

头歌计算机组成原理实验—运算器设计(9)第9关:原码一位乘法器设计

第9关&#xff1a;原码一位乘法器设计 实验目的 学生掌握原码一位乘法运算的基本原理&#xff0c;熟练掌握 Logisim 寄存器电路的使用&#xff0c;能在 Logisim 平台中设计实现一个 8*8位的无符号数乘法器。 视频讲解 ####实验内容 在 alu.circ 文件中的原码一位乘法器子电…

异常和中断

异常和中断机制 ​ 现代计算机中都配有完善的异常和中断处理系统&#xff0c;CPU的数据通路中有相应的异常检测和响应逻辑&#xff0c;外设接口中有相应的中断请求和控制逻辑&#xff0c;操作系统中有相应的中断服务程序。 异常和中断的基本概念 异常&#xff08;内中断&#…

ansible使用parted划分磁盘报Error: Partition(s) on /dev/sda are being used.

使用ansible划分磁盘报错&#xff1a;Error: Partition(s) on /dev/sda are being used. FAILED! > {"changed": false, "err": "Error: Partition(s) on /dev/sda are being used.\n", "msg": "Error while running parted …

Linux【Ubuntu】安装Docker配置docker-compose 编排工具

一&#xff1a;Docker具体安装传送门: 亲测有效 https://www.runoob.com/docker/ubuntu-docker-install.html 二&#xff1a;配置Docker编排工具docker-compose 1&#xff0c;下载Docker-compose 下载Docker-Compose&#xff08;下载完毕就是一个文件docker-compose-Linux-x…