C++ lambda表达式详解
创始人
2024-03-22 07:08:41
0

一、lambda表达式基本用法

1、语法

Lambda 表达式的基本语法如下:
捕获列表 mutable(可选) 异常属性 -> 返回类型
{
// 函数体
}

2、lambda值捕获

/*** @brief lamdba值捕获*/
void test()
{int nvalue = 1;auto func_copyvalue = [nvalue]{return nvalue;};nvalue = 100;auto nValue = func_copyvalue();cout << "value:" << nValue << endl;// 这时, nValue == 1, 而 nvalue == 100.// 因为 func_copyvalue 在创建时就保存了一份 nvalue 的拷贝
}

3、lambda引用捕获

/*** @brief lamdba值捕获*/
void test()
{int nvalue = 1;auto func_copyvalue = [&nvalue]{return nvalue;};nvalue = 100;auto nValue = func_copyvalue();cout << "value:" << nValue << endl;// 这时, nValue == 100, 而 nvalue == 100.// 因为 func_copyvalue 保存的是引用
}

4、lambda隐式捕获

/*
[] 空捕获列表
[name1, name2, ...] 捕获一系列变量
[&] 引用捕获, 让编译器自行推导引用列表
[=] 值捕获, 让编译器自行推导值捕获列表
*/
void test()
{// [] 空捕获列表auto func1 = [](int a) {return a; };// [name1, name2, ...] 捕获一系列变量int name1 = 1;int name2 = 2;int name3 = 3;auto func2 = [name1, name2, name3]() {return name1 + name2 + name3; };// [&] 引用捕获, 让编译器自行推导引用列表auto func3 = [&]() {return name1 + func1(1); };// [=] 值捕获, 让编译器自行推导值捕获列表auto func4 = [=]() {return name1; };
}

5、lambda表达式捕获

上面提到的值捕获、引用捕获都是已经在外层作用域声明的变量,因此这些捕获方式捕获的均为左值,而不能捕获右值。
C++14 给与了我们方便,允许捕获的成员用任意的表达式进行初始化,这就允许了右值的捕获, 被声明的捕获变量类型会根据表达式进行判断,判断方式与使用 auto 本质上是相同的

#include 
#include 
void test()
{auto important = std::make_unique(1);auto add = [v1 = 1, v2 = std::move(important)](int x, int y)->int{return x + y + v1 + (*v2);};cout << add(3, 4) << endl;
}

6、泛型lambda

void test()
{auto add = [](auto x, auto y) {return x + y; };cout << add(3, 4) << endl;cout << add(1, 2) << endl;cout << add(1.1, 2.3) << endl;
}

二、lambda表达式与algorithm相结合使用(记录常用的)

1、std::sort

#include 
#include 
void test()
{std::vector vec{1,3,5,2,4,7,9,8,10};std::sort(vec.begin(),vec.end(),[](int a, int b){return a < b;});
}

2、std::for_each

#include 
#include 
void test()
{std::vector vecTest{1,3,5,2,4,7,9,8,10};std::for_each(vecTest.begin(), vecTest.end(), [](int n) { return n; });
}

3、std::copy

#include 
#include 
void test()
{std::vector m_Vec{1,3,5,2,4,7,9,8,10};std::copy(m_Vec.begin(), m_Vec.end(), ostream_iterator(cout, " "));
}

4、std::function

#include 
#include 
#include 
void test()
{std::map> op_dict = {{"+", [](int x, int y){return x + y;}},{"-", [](int x, int y){return x - y;}},{"*", [](int x, int y){return x * y;}},{"/", [](int x, int y){return x / y;}},};
}

5、std::find_if

#include 
#include 
void test()
{std::vector vec{ 1,2,3,5,3,7,9,5,10 };auto iter = std::find_if(vec.begin(), vec.end(),[](int a){return a > 5;});
}

6、std::count_if

#include 
#include 
void test()
{std::vector vec{ 1,2,3,5,3,7,9,5,10 };auto iter = std::count_if(vec.begin(), vec.end(),[](int a){return a > 5;});
}

三、lambda在线程中使用

#include 
#include using namespace std;int main() 
{std::thread tthread001([](int x){cout << x << endl;}, 100);tthread001.join();return 0;
}

相关内容

热门资讯

保存时出现了1个错误,导致这篇... 当保存文章时出现错误时,可以通过以下步骤解决问题:查看错误信息:查看错误提示信息可以帮助我们了解具体...
汇川伺服电机位置控制模式参数配... 1. 基本控制参数设置 1)设置位置控制模式   2)绝对值位置线性模...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
表格中数据未显示 当表格中的数据未显示时,可能是由于以下几个原因导致的:HTML代码问题:检查表格的HTML代码是否正...
本地主机上的图像未显示 问题描述:在本地主机上显示图像时,图像未能正常显示。解决方法:以下是一些可能的解决方法,具体取决于问...
表格列调整大小出现问题 问题描述:表格列调整大小出现问题,无法正常调整列宽。解决方法:检查表格的布局方式是否正确。确保表格使...
不一致的条件格式 要解决不一致的条件格式问题,可以按照以下步骤进行:确定条件格式的规则:首先,需要明确条件格式的规则是...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...