本文主要是介绍C++之lambda匿名函数总结(二百四十五),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!
优质专栏:Audio工程师进阶系列【原创干货持续更新中……】🚀
人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.
1.前言
本篇目的:理解C++之lambda匿名函数用法总结。
2.C++11之lambda匿名函数实例总结
-
Lambda函数是C++11引入的一种匿名函数的方式,它可以在需要函数对象的地方快速定义一个临时的函数。Lambda函数的语法相对简洁,可以方便地捕获局部变量,并且可以作为参数传递给其他函数。
-
Lambda函数的基本语法如下:
[capture](parameters) -> return_type {// 函数体
}
- 其中,
capture
是指捕获的变量列表,可以是空的、值传递方式或引用传递方式。parameters
是形参列表,可以为空或包含一个或多个形参。return_type
是返回类型,可以省略(根据函数体自动推断)或明确指定。函数体
是具体的函数实现。
以下是Lambda函数的一些常见用法和特性:
-
捕获变量:Lambda函数可以通过捕获变量来访问其作用域外的变量。捕获方式包括值捕获(通过值传递方式访问变量)和引用捕获(通过引用传递方式访问变量)。
-
自动类型推断:Lambda函数可以根据函数体自动推断返回类型,也可以显式指定返回类型。
-
函数重载:Lambda函数可以像普通函数一样进行重载,可以有多个具有相同函数体的Lambda函数,只需在参数列表中有所区别即可。
-
内联调用:Lambda函数通常是在定义的地方即被调用,也可以将Lambda函数作为参数传递给其他函数进行调用。
-
使用标准算法库:Lambda函数经常与标准算法库中的函数(如sort、find_if、transform等)配合使用,提供灵活的函数对象。
3.C++11之lambda匿名函数实例
1. 捕获变量、按照特定规则排序
#include <iostream>
#include <vector>
#include <algorithm>int main() {int a = 5;int b = 3;std::vector<int> nums = {2, 5, 1, 7, 3};// 捕获变量 a 和引用捕获变量 b,并按照特定规则排序std::sort(nums.begin(), nums.end(), [a, &b](int x, int y) {return a * x < b * y;});// 输出排序后的结果for (int num : nums) {std::cout << num << " ";}return 0;
}
2.自动类型推断并显式指定返回类型
#include <iostream>int main() {// 自动类型推断的 Lambda 函数auto lambda1 = [](auto x, auto y) {return x + y;};// 显式指定返回类型的 Lambda 函数auto lambda2 = [](int x) -> int {return x * x;};// 使用 Lambda 函数进行计算并输出结果std::cout << "自动类型推断的结果:" << lambda1(3, 4) << std::endl;std::cout << "显式指定返回类型的结果:" << lambda2(5) << std::endl;return 0;
}
3. 函数重载并内联调用
#include <iostream>// Lambda 函数重载
auto lambda1 = [](int x) -> int {return x * x;
};auto lambda2 = [](double x) -> double {return x * x;
};// 具体函数内联调用 Lambda 函数
inline void printSquare(int x, auto squareFunc) {std::cout << "调用的结果为:" << squareFunc(x) << std::endl;
}int main() {int num1 = 5;double num2 = 2.5;printSquare(num1, lambda1); // 调用 lambda1 计算 num1 的平方printSquare(num2, lambda2); // 调用 lambda2 计算 num2 的平方return 0;
}
4.匿名函数内联调用
#include <iostream>int main() {int num1 = 5;int num2 = 2;// 使用Lambda函数计算平方并输出结果auto printSquare = [&]() {int square1 = num1 * num1; // 计算num1的平方int square2 = num2 * num2; // 计算num2的平方std::cout << "num1的平方:" << square1 << std::endl;std::cout << "num2的平方:" << square2 << std::endl;};// 调用Lambda函数printSquare();return 0;
}
5.lambda 函数作为参数调用schedule 函数
#include <iostream>
#include <functional>
//schedule的参数没有返回值和参数
void schedule(const std::function<void()>& func) {func();int main() {int x = 10;Scheduler scheduler;// 使用 lambda 函数作为参数调用 schedule 函数schedule([=] {std::cout << "The value of x is: " << x << std::endl;});return 0;
}
6.类中的lambda函数作为参数调用schedule 函数
#include <iostream>
#include <functional>class Scheduler {public://schedule的参数没有返回值和参数void schedule(const std::function<void()>& func) {func();}};int main() {int x = 10;Scheduler scheduler;// 使用lambda成员函数作为参数调用schedule函数scheduler.schedule([=] {schedule([=] {std::cout << "The value of x is: " << x << std::endl;});return 0;
}
7.lambda函数作为参数调用schedule函数,并将返回值放在匿名回调函数中返回。
void schedule(const std::function<void(int x)>& func) {int ret = 100;func(100);}int main() {int x = 10;//schedule([&](int y) {std::cout << "The value of x is: " << x << std::endl;std::cout << "The value of y is: " << y << std::endl;});return 0;
}
8.lambda匿名函数带三个参数返回,并将返回的值赋值给a、b、c。
#include <iostream>
#include <functional>void callback_test(const std::function<void(int x, int y, int z)>& func) {int ret1 = 100;int ret2 = 200;int ret3 = 300;//func传入三个参数,相当于调用匿名的lambda回调函数,这里传入三个参数.func(ret1, ret2, ret3);
}int main() {int a,b,c;callback_test([&](int x, int y, int z){a = x;b = y;c = z;printf("xxx------------>%s(), line = %d, x = %d, y = %d, z = %d\n",__FUNCTION__,__LINE__,x,y,z);});printf("xxx------------>%s(), line = %d, a = %d, b = %d, c = %d\n",__FUNCTION__,__LINE__,a,b,c);return 0;
}
总结:
此处lambda回调函数,本质是从callback_test获取数据,使用func(ret1,ret2,ret3)本质调用lambda匿名回调函数,将值传出来。
9.回调函数传入lambda匿名函数和普通函数
#include <iostream>
#include <functional>//v1.0
void callback_test(const std::function<void(int &x, int &y, int &z)>& func) {int ret1 = 100;int ret2 = 200;int ret3 = 300;//func传入三个参数,相当于调用匿名的lambda回调函数,这里传入三个参数.func(ret1, ret2, ret3);
}void test(int &a,int &b, int &c){printf("xxx------------>%s(), line = %d, a = %d, b = %d, c = %d\n",__FUNCTION__,__LINE__,a,b,c);
}int main() {//v1.0: lambda为匿名函数,直接使用int a,b,c;callback_test([&](int x, int y, int z){a = x;b = y;c = z;printf("xxx------------>%s(), line = %d, x = %d, y = %d, z = %d\n",__FUNCTION__,__LINE__,x,y,z);});printf("xxx------------>%s(), line = %d, a = %d, b = %d, c = %d\n",__FUNCTION__,__LINE__,a,b,c);//v2.0:将普通函数传入,返回a,b,c的值.callback_test(test);return 0;
}
这篇关于C++之lambda匿名函数总结(二百四十五)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!