Lambda表达式默认的返回类型为void
  为了对比,下面使用函数对象实现相同功能的代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<ostream>
#include<cassert>
using namespace std;
class LambdaFunctor{
public:
void operator()(int n) const{
cout << n << " ";
}
};
int main()
{
vector<int> v;
for (int i = 0; i < 10; i++){
v.push_back(i);
}
for_each(v.begin(), v.end(), LambdaFunctor());
cout << endl;
return 0;
}
  对比一下,会发现使用Lambda表达式要简洁得多
  上面提到的Lambda表达式可以操作所在作用域的变量,这需要通过被称为“捕获”的特殊语法来实现,是通过在[]内列出将要捕获的“外部”变量列表,这样在函数体内可以访问并操作这些变量。参考下面的代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<ostream>
#include<cassert>
using namespace std;
int main()
{
vector<int> v;
for (int i = 0; i < 10; i++){
v.push_back(i);
}
//使用for_each语句和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;
}
});
cout << "There are " << evenCount << " even numbers in the vector." << endl;
getchar();
return 0;
}