forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.cpp
36 lines (28 loc) · 836 Bytes
/
lambda.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
# lambda
C++11
Function without name.
Specialy useful in conjunction with function that take functions as arguments such as `std::find_if`,
when we only want to use the function passed once.
Good explanation: <http://stackoverflow.com/a/7627218/895245>
TODO: why square brackets? Why not pass by reference as in a regular function?
*/
#include "common.hpp"
int main() {
#if __cplusplus >= 201103L
{
int i = 0;
int j = 0;
auto f = [&i,&j](int k) -> int {i = 1; j = 1; return k + 1;};
assert(f(0) == 1);
assert(i == 1);
assert(j == 1);
}
// Typical application with find_if.
{
std::vector<int> v = {2, 0, 1};
assert(std::find_if(v.begin(), v.end(),
[](int i) {return i % 2 == 1;}) == --v.end());
}
#endif
}