在 Visual C++ 中,lambda 表达式引用为,在将 lambda与维护状态的匿名函数并可以对封闭范围可用的变量。 本文定义了 lambda 是,与其他编程技术对它们进行比较,描述其优点,并提供一个基本示例。
许多编程语言支持 匿名函数的概念,这些函数有主体,但是,没有名称。 lambda 是使用匿名功能相关的编程方法。 lambda 隐式定义函数对象类和构造函数对象。选件类类型。 有关函数对象的更多信息,请参见 函数对象"。
![]() |
---|
要在下列公共语言运行时 (CLR) 托管实体不受支持: ref class、 ref struct、 value class或 value struct。 |
在编写代码时,您可能使用函数指针和函数对象解决问题和执行计算,特别是当您使用 STL 算法。 函数指针和函数对象有优点和缺点。例如,函数指针具有最低的语法开销,但不保留在范围内的状态,而函数对象能够维护状态,但需要类定义的语法开销。
将 lambda 函数指针和函数对象的优点并避免其缺点。 象函数对象,lambda 是灵活,并且可以维护状态,但是,不同函数对象,其简洁语法不需要类定义。 使用lambda,相比等效的函数对象代码,您可以写出不太复杂并且不容易出错的代码。
下面的示例比较lambda和函数对象的使用。 第一个示例使用 lambda 打印到控件个在 vector 对象的每个元素是否均匀或更多的。 第二个示例使用函数对象来完成相同任务。
此示例使用在 for_each 嵌入函数调用打印到控件中的 lambda 在 vector 对象的每个元素是否均匀或更多的。
代码
// 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 10 elements. vector<int> v; for (int i = 0; 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
0 is even 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 5 even numbers in the vector.
注释
在此示例中, for_each 函数的第三个参数是一个lambda。 [&evenCount] 部件来指定表达式中获取子句,(int n) 指定参数,因此,其余部分来指定表达式的主体。
有时 lambda 比前面大麻烦的以致无法扩展进一步。 下一个示例使用函数对象而不是 lambda,与 for_each 功能外,还生成结果和示例 1. 相同。 两个示例在 vector 对象存储计数偶数。 若要维护操作的状态,FunctorClass 选件类存储 m_evenCount 变量引用作为成员变量。 若要执行操作,FunctorClass 实现函数调用运算符,operator()。 Visual C++ 编译器生成与示例 1. 中的 lambda 代码的大小是可比的和性能的代码。 对于基本的问题比函数对象模型 (如一个本文中,更简单的 lambda 模型可能好。 但是,因此,如果您认为功能可能在将来需要大量展开,然后使用函数对象模型,以便代码维护会更加容易。
有关 operator() 的更多信息,请参见 函数调用(C++)。 有关 for_each 函数的更多信息,请参见 for_each。
代码
// 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 10 elements. vector<int> v; for (int i = 0; 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; }
Lambda 非常强大表达编程技术。 若要了解 lambda 内容和属性,请参见 Lambda表达式语法。 若要了解如何使用 lambda 在您的程序,请参见 Lambda表达式的示例。
http://msdn.microsoft.com/zh-CN/library/vstudio/dd293608.aspx