结合C++11新特性来学习C++中lambda表达式的用法
在 C++ 11 中,lambda 表达式(通常称为 "lambda")是一种在被调用的位置或作为参数传递给函数的位置定义匿名函数对象的简便方法。 Lambda 通常用于封装传递给算法或异步方法的少量代码行。 本文定义了 lambda 是什么,将 lambda 与其他编程技术进行比较,描述其优点,并提供一个基本示例。 #include <algorithm> #include <cmath> void abssort(float* x,unsigned n) { std::sort(x,x + n,// Lambda expression begins [](float a,float b) { return (std::abs(a) < std::abs(b)); } // end of lambda expression ); } 此图显示了 lambda 的组成部分:
Capture 子句 Lambda 可在其主体中引入新的变量(用 C++14),它还可以访问(或“捕获”)周边范围内的变量。 Lambda 以 Capture 子句(标准语法中的 lambda 引导)开头,它指定要捕获的变量以及是通过值还是引用进行捕获。 有与号 (&) 前缀的变量通过引用访问,没有该前缀的变量通过值访问。 [&total,factor] [factor,&total] [&,&] [=,&total] [&total,=] 使用 capture-default 时,只有 lambda 中提及的变量才会被捕获。 struct S { void f(int i); }; void S::f(int i) { [&,i]{}; // OK [&,&i]{}; // ERROR: i preceded by & when & is the default [=,this]{}; // ERROR: this when = is the default [i,i]{}; // ERROR: i repeated } capture 后跟省略号是包扩展,如以下可变参数模板示例中所示: template<class... Args> void f(Args... args) { auto x = [args...] { return g(args...); }; x(); } 要在类方法的正文中使用 lambda 表达式,请将 this 指针传递给 Capture 子句,以提供对封闭类的方法和数据成员的访问权限。 有关展示如何将 lambda 表达式与类方法一起使用的示例,请参阅 Lambda 表达式的示例中的“示例:在方法中使用 Lambda 表达式”。 通用捕获 (C++14) pNums = make_unique<vector<int>>(nums); //... auto a = [ptr = move(pNums)]() { // use ptr }; 参数列表 int y = [] (int first,int second) { return first + second; }; 在 C++14 中,如果参数类型是泛型,则可以使用 auto 关键字作为类型说明符。 这将告知编译器将函数调用运算符创建为模板。 参数列表中的每个 auto 实例等效于一个不同的类型参数。 auto y = [] (auto first,auto second) { return first + second; }; lambda 表达式可以将另一个 lambda 表达式作为其参数。 有关详细信息,请参阅 Lambda 表达式的示例主题中的“高阶 Lambda 表达式”。 可变规范 // throw_lambda_expression.cpp // compile with: /W4 /EHsc int main() // C4297 expected { []() throw() { throw 5; }(); } 返回类型 auto x1 = [](int i){ return i; }; // OK: return type is int auto x2 = []{ return{ 1,2 }; }; // ERROR: return type is void,deducing // return type from braced-init-list is not valid lambda 表达式可以生成另一个 lambda 表达式作为其返回值。 有关详细信息,请参阅 Lambda 表达式的示例中的“高阶 Lambda 表达式”。
以下示例包含通过值显式捕获变量 n 并通过引用隐式捕获变量 m 的 lambda 表达式: // captures_lambda_expression.cpp // compile with: /W4 /EHsc #include <iostream> using namespace std; int main() { int m = 0; int n = 0; [&,n] (int a) mutable { m = ++n + a; }(4); cout << m << endl << n << endl; } 输出: 5 0 void fillVector(vector<int>& v) { // A local static variable. static int nextValue = 1; // The lambda expression that appears in the following call to // the generate function modifies and uses the local static // variable nextValue. generate(v.begin(),v.end(),[] { return nextValue++; }); //WARNING: this is not thread-safe and is shown for illustration only } 下面的代码示例使用上一示例中的函数,并添加了使用 STL 算法 generate_n 的 lambda 表达式的示例。 该 lambda 表达式将 vector 对象的元素指派给前两个元素之和。 使用了 mutable 关键字,以使 lambda 表达式的主体可以修改 lambda 表达式通过值捕获的外部变量 x 和 y 的副本。 由于 lambda 表达式通过值捕获原始变量 x 和 y,因此它们的值在 lambda 执行后仍为 1。 // compile with: /W4 /EHsc #include <algorithm> #include <iostream> #include <vector> #include <string> using namespace std; template <typename C> void print(const string& s,const C& c) { cout << s; for (const auto& e : c) { cout << e << " "; } cout << endl; } void fillVector(vector<int>& v) { // A local static variable. static int nextValue = 1; // The lambda expression that appears in the following call to // the generate function modifies and uses the local static // variable nextValue. generate(v.begin(),[] { return nextValue++; }); //WARNING: this is not thread-safe and is shown for illustration only } int main() { // The number of elements in the vector. const int elementCount = 9; // Create a vector object with each element set to 1. vector<int> v(elementCount,1); // These variables hold the previous two elements of the vector. int x = 1; int y = 1; // Sets each element in the vector to the sum of the // previous two elements. generate_n(v.begin() + 2,elementCount - 2,[=]() mutable throw() -> int { // lambda is the 3rd parameter // Generate current value. int n = x + y; // Update previous two values. x = y; y = n; return n; }); print("vector v after call to generate_n() with lambda: ",v); // Print the local variables x and y. // The values of x and y hold their initial values because // they are captured by value. cout << "x: " << x << " y: " << y << endl; // Fill the vector with a sequence of numbers fillVector(v); print("vector v after 1st call to fillVector(): ",v); // Fill the vector with the next sequence of numbers fillVector(v); print("vector v after 2nd call to fillVector(): ",v); } 输出: vector v after call to generate_n() with lambda: 1 1 2 3 5 8 13 21 34 x: 1 y: 1 vector v after 1st call to fillVector(): 1 2 3 4 5 6 7 8 9 vector v after 2nd call to fillVector(): 10 11 12 13 14 15 16 17 18 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |