Linux
Linux software install
Google repo的使用
Vector AP StartApplication编译脚本解析
Yocto的cmake版本升级
不能自动安装的解决方式
S32G-BSP35.0-SDK使用方法
S32G从SDK生成文件系统的制作过程
Linux Samba设置
Linux添加双网卡
S32G USB-Redirector安装指南
VS code自动生成Doxygen格式注释
Linux下使用多线程下载
使用pandoc 生成带中文的pdf
minicom无法输入的问题解决办法
使用 systemd unit 添加一条路由规则
CMake 教程
步骤 1:基本起点
步骤 2:添加lib库
步骤 3:为库添加使用要求
步骤 4:添加生成器表达式
步骤 5:安装和测试
步骤 6:添加支持测试仪表板
步骤 7: 添加系统内省
步骤 8:自定义命令和生成的文
步骤 9:打包安装程序
步骤 10:选择静态库或共享库
步骤 11:添加导出配置
步骤 12:打包 Debug 和 Release
添加虚拟网卡
Vector AP 去掉防篡改校验
Vector AP startapplication编译与使用
Vector AP问题汇总
Vector AP大型项目开发流程
Vector AP EM
Vector AP 最简单的开发示例
Linux kernel 版本降级
Vector AP StartApplicaiton
startappplication-machine-design
startapplicaiton-machine-integration
amsr-vector-fs-em-executionmanager-design
amsr-vector-fs-em-executionmanager
Vector AP 复杂模型的开发
第一章 Machine和MachineDesign
第二章 Execute Manager
第三章 Log
第四章 State Manager
第五章 State Manager 源码的理解
第六章 Someip daemon
第七章 IPC Service Discovery Daemon
crond的使用方法
解决蓝牙鼠标在 Ubuntu 中单位时间内断开的问题
VPS服务器自建教程
v2rayA的客户端使用配置
GDB调试指南入门篇:揭开程序运行世界的神秘面纱
GDB调试指南高级篇:成为调试专家,掌控程序的命运
Linux安装PyQt5并配置Qt Designer
ADB 命令大全
GoogleTest(一)
GoogleTest(二)简单的TEST宏
GoogleTest(三)简单类函数测试
C++ Template
1. 函数模板
2. 类模板
3. 非类型模板参数
软件版本号规范
EPOLL
C++手札
C++ 使用{}初始化有哪些好处?
现代 C++ decltype 深入解析
函数对象(functor)
Linux性能剖析:CPU、内存、网络与I/O压力测试
AP StateManager
C++ Lambda表达式
C++ 中的Lambda表达式
Lambda 表达式语法
Lambda 表达式的示例
手动发送UDP数据包
pyqt5生成的UI界面不能输入中文
自己搭建repo镜像
摄影
Sony仿富士PP值设置
诗词歌赋
本文档使用 MrDoc 发布
-
+
首页
Lambda 表达式语法
## 本文内容 1. [函数对象与 lambda](#function-objects-vs-lambdas) 2. [示例 1:使用 lambda](#example-1-using-a-lambda) 3. [示例 2:使用函数对象](#example-2-using-a-function-object) 4. [另请参阅](#see-also) 本文演示了 lambda 表达式的语法和结构化元素。 ## 函数对象与 lambda 当你编写代码时,可能会使用函数指针和函数对象来解决问题和执行计算,尤其是当使用 [C++ 标准库算法](https://learn.microsoft.com/zh-cn/cpp/standard-library/algorithms?view=msvc-170)时。 函数指针和函数对象各有利弊。例如,函数指针具有最低的语法开销,但不保持范围内的状态,函数对象可保持状态,但需要类定义的语法开销。 lambda 结合了函数指针和函数对象的优点并避免其缺点。 与函数对象一样,lambda 是灵活的并且可以保持状态,但与函数对象不同之处在于其简洁的语法不需要显式类定义。 使用 lambda,你可以编写出比等效的函数对象代码更简洁、更不容易出错的代码。 以下示例将比较 lambda 的用途和函数对象的用途。 第一个示例使用 lambda 向控制台打印 `vector` 对象中的每个元素是偶数还是奇数。 第二个示例使用函数对象来完成相同任务。 ## 示例 1:使用 lambda 此示例将一个 lambda 传递给 for\_each 函数。 该 lambda 打印一个结果,该结果指出 `vector` 对象中的每个元素是偶数还是奇数。 ### 代码 ```cpp // even_lambda.cpp // compile with: cl /EHsc /nologo /W4 /MTd #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { // Create a vector object that contains 9 elements. vector<int> v; for (int i = 1; i < 10; ++i) { v.push_back(i); } // Count the number of even numbers in the vector by // using the for_each function and a lambda. int evenCount = 0; for_each(v.begin(), v.end(), [&evenCount] (int n) { cout << n; if (n % 2 == 0) { cout << " is even " << endl; ++evenCount; } else { cout << " is odd " << endl; } }); // Print the count of even numbers to the console. cout << "There are " << evenCount << " even numbers in the vector." << endl; } ``` Output ```shell 1 is odd 2 is even 3 is odd 4 is even 5 is odd 6 is even 7 is odd 8 is even 9 is odd There are 4 even numbers in the vector. ``` ### 注释 在此示例中,for\_each 函数的第三个参数是一个 lambda。 `[&evenCount]` 部分指定表达式的捕获子句,`(int n)` 指定参数列表,剩余部分指定表达式的主体。 ## 示例 2:使用函数对象 有时 lambda 过于庞大,无法在上一示例的基础上大幅度扩展。 下一示例将函数对象(而非 lambda)用于 for\_each 函数以产生与示例 1 相同的结果。 两个示例都在 `vector` 对象中存储偶数的个数。 为保持运算的状态,`FunctorClass` 类通过引用存储 `m_evenCount` 变量作为成员变量。 为了执行运算,`FunctorClass` 将实现函数调用运算符 operator()。 Microsoft C++ 编译器生成的代码与示例 1 中的 lambda 代码在大小和性能上相差无几。 对于类似本文中示例的基本问题,较为简单的 lambda 设计可能优于函数对象设计。 但是,如果你认为该功能在将来可能需要重大扩展,则使用函数对象设计,这样代码维护会更简单。 有关 operator() 的详细信息,请参阅[函数调用](https://learn.microsoft.com/zh-cn/cpp/cpp/function-call-cpp?view=msvc-170)。 有关 for\_each 函数的详细信息,请参阅 [for\_each](https://learn.microsoft.com/zh-cn/cpp/standard-library/algorithm-functions?view=msvc-170#for_each)。 ### 代码 ```cpp // even_functor.cpp // compile with: /EHsc #include <algorithm> #include <iostream> #include <vector> using namespace std; class FunctorClass { public: // The required constructor for this example. explicit FunctorClass(int& evenCount) : m_evenCount(evenCount) { } // The function-call operator prints whether the number is // even or odd. If the number is even, this method updates // the counter. void operator()(int n) const { cout << n; if (n % 2 == 0) { cout << " is even " << endl; ++m_evenCount; } else { cout << " is odd " << endl; } } private: // Default assignment operator to silence warning C4512. FunctorClass& operator=(const FunctorClass&); int& m_evenCount; // the number of even variables in the vector. }; int main() { // Create a vector object that contains 9 elements. vector<int> v; for (int i = 1; i < 10; ++i) { v.push_back(i); } // Count the number of even numbers in the vector by // using the for_each function and a function object. int evenCount = 0; for_each(v.begin(), v.end(), FunctorClass(evenCount)); // Print the count of even numbers to the console. cout << "There are " << evenCount << " even numbers in the vector." << endl; } ``` Output ```shell 1 is odd 2 is even 3 is odd 4 is even 5 is odd 6 is even 7 is odd 8 is even 9 is odd There are 4 even numbers in the vector. ``` ## 另请参阅 [Lambda 表达式](https://learn.microsoft.com/zh-cn/cpp/cpp/lambda-expressions-in-cpp?view=msvc-170) [Lambda 表达式的示例](https://learn.microsoft.com/zh-cn/cpp/cpp/examples-of-lambda-expressions?view=msvc-170) [generate](https://learn.microsoft.com/zh-cn/cpp/standard-library/algorithm-functions?view=msvc-170#generate) [generate\_n](https://learn.microsoft.com/zh-cn/cpp/standard-library/algorithm-functions?view=msvc-170#generate_n) [for\_each](https://learn.microsoft.com/zh-cn/cpp/standard-library/algorithm-functions?view=msvc-170#for_each) [异常规范 (throw)](https://learn.microsoft.com/zh-cn/cpp/cpp/exception-specifications-throw-cpp?view=msvc-170) [编译器警告(等级 1)C4297](https://learn.microsoft.com/zh-cn/cpp/error-messages/compiler-warnings/compiler-warning-level-1-c4297?view=msvc-170) [Microsoft 专用的修饰符](https://learn.microsoft.com/zh-cn/cpp/cpp/microsoft-specific-modifiers?view=msvc-170)
admin
2024年8月28日 13:58
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码