C++11里面的Lambda表达式 (二)

2014-11-23 22:08:32 ? 作者: ? 浏览: 6
W4 /MTd
#include
#include
#include
using namespace std;

int main()
{
// Create a vector object that contains 10 elements.
vector 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.

Comments
In the example, the third argument to the for_each function is a lambda. The [&evenCount] part specifies the capture clause of the expression, (int n) specifies the parameter list, and remaining part specifies the body of the expression.

在这个例子中,第三个for_each的参数是一个lambda表达式。所谓[&evenCount]部分,明确说明了分句表达式,(int n)明确说明了一个形参(参数?)列表,剩余部分是表达式的主体。
Example 2: Using a Function Object
Sometimes a lambda would be too unwieldy to extend much further than the previous example. The next example uses a function object instead of a lambda, together with the for_each function, to produce the same results as Example 1. Both examples store the count of even numbers in a vector object. To maintain the state of the operation, the FunctorClass class stores the m_evenCount variable by reference as a member variable. To perform the operation, FunctorClass implements the function-call operator, operator(). The Visual C++ compiler generates code that is comparable in size and performance to the lambda code in Example 1. For a basic problem like the one in this article, the simpler lambda design is probably better than the function-object design. However, if you think that the functionality might require significant expansion in the future, then use a function object design so that code maintenance will be easier.

For more information about the operator(), see Function Call (C++). For more information about the for_eachfunction, see for_each.

有时候lambda表达式过于繁杂(扩充功能)。下一个例子采用了函数对象,同样使用for_each。都是数vector元素中偶数个数,为保持操作状态,FunctorClass类保存了m_evenCount变量作为一个成员变量的引用。为实现功能,FunctorClass重载(implement?)了function-call操作符,operator()。VC++编译器产生了与之相当大小和表现得lambda表达式代码(例一)。在这个基本问题中,简易的lambda表达式设计可能比函数对象更好。然而,若你认为functionality(函数性?)可能需要一个明确的扩展,那么就用函数对象写代码,那会简单些。
欲知operator()和for_each,参考Function Call(C++),for_each,在MSDN库内有。
[cpp]
// even_functor.cpp
// compile with: /EHsc
#include
#include
#include
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 v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}

// Count the number of even numbers in

-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: