-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathlambda.cc
More file actions
79 lines (72 loc) · 1.9 KB
/
lambda.cc
File metadata and controls
79 lines (72 loc) · 1.9 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <cstddef>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
void fcn1()
{
size_t v1 = 42;
auto f = [v1]{ return v1; };
v1 = 0;
auto j = f();
std::cout << "v1: " << v1 << " j: " << j << std::endl;
}
void fcn2()
{
size_t v1 = 42;
auto f2 = [&v1] { return v1; };
v1 = 0;
auto j = f2();
std::cout << "v1: " << v1 << " j: " << j << std::endl;
}
void fcn4()
{
size_t v1 = 42;
// v1 is a reference to a non const variable,
// we can change that variable through the reference inside f2
auto f2 = [&v1] { return ++v1; };
v1 = 0;
auto j = f2();
}
void biggies(
std::vector<std::string> &words,
std::vector<std::string>::size_type sz,
std::ostream &os = std::cout,
char c = ' ')
{
std::for_each(words.begin(), words.end(),
[&os, c](const std::string &s) { os << s << c; });
// [&, c] (const std::string &s) { os << s << c; }
// [=, &os] (const std::string &s) { os << s << c; }
}
void fcn3()
{
size_t v1 = 42;
auto f= [v1]() mutable { return ++v1; };
v1 = 0;
auto j = f();
std::cout << "v1: " << v1 << " j: " << j << std::endl;
}
int main()
{
auto f = []{ return 42; };
std::cout << f() << std::endl;
fcn1();
fcn2();
fcn3();
std::vector<std::string> words{"the","quick","red","fox","jumps","over","the","slow","red","turtle"};
int sz = 4;
biggies(words, sz);
auto wc = std::find_if(words.begin(), words.end(),
[=](const std::string &s) { return s.size() >= sz; });
int ia[] = {27, 210, 12, 47, 109, 83};
std::transform(std::begin(ia), std::end(ia), std::begin(ia),
[](int i) { return i < 0 ? -i : i; });
std::transform(std::begin(ia), std::end(ia), std::begin(ia),
[](int i) -> int {
if (i < 0) return -i;
else return i;
});
return 0;
}