intmain() { usingnamespacestd; list<int> numbers; numbers.push_back(13); numbers.push_back(17); numbers.push_back(42); numbers.push_back(46); numbers.push_back(99); constlist<int>::const_iterator result = find_if(numbers.begin(), numbers.end(),[](int n) { return (n % 2) == 0; }); if (result != numbers.end()) { cout << "The first even number in the list is " << *result << "." << endl; // 第一个偶数是 42 } else { cout << "The list contains no even numbers." << endl; } }
嵌套 Lambda 表达式
1 2 3 4 5 6 7 8 9 10 11 12 13
#include<iostream>
intmain() { usingnamespacestd; // The following lambda expression contains a nested lambda // expression. int timestwoplusthree = [](int x) { return [](int y) { return y * 2; }(x) + 3; }(5); // Print the result. cout << timestwoplusthree << endl; }