-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlambdaexamples.cpp
More file actions
81 lines (59 loc) · 1.6 KB
/
lambdaexamples.cpp
File metadata and controls
81 lines (59 loc) · 1.6 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
80
81
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> // for_each
#include <functional> // function
using namespace std;
void example1();
void example2();
void example3();
int main() {
example1();
example2();
example3();
return 0;
}
// C++의 람다식을 설명하기 위한 예제
void example1() {
auto lambda1 = [](const string& a, const string& b) -> bool
{ return a.size() < b.size(); };
int sz = 10;
// auto lambda2 = [sz](const string& a) { return a.size() >= sz; };
// capture list에서 & 사용
auto lambda2 = [&sz](const string& a) { return a.size() >= sz; };
cout << lambda1("abc", "def ghi") << endl;
cout << lambda2("hello") << endl;
sz = 3;
cout << lambda2("hello") << endl;
}
// 람다의 활용예 1번: 템플릿의 인자
void example2() {
vector<string> words;
words.push_back("a");
words.push_back("de");
words.push_back("ghi");
char c = ' ';
ostream& os = cout;
for_each(words.begin(), words.end(),
[&os, c](const string& s) { cout << s << c; });
sort(words.begin(), words.end(),
[](const string& s1, const string& s2) { return s1 > s2; });
for_each(words.begin(), words.end(),
[&os, c](const string& s) { cout << s << c; });
int num = 2;
cout <<
count_if(words.begin(), words.end(),
[num](const string& s) { return s.length() < num; })
<< endl;
}
// 람다의 활용예 2번: 고차원 함수
void example3() {
auto addtwointegers = [](int x) -> function<int(int)> {
return [=](int y) { return x + y; };
};
auto higherorder = [](const function<int(int)>& f, int z) {
return f(z) * 2;
};
auto answer = higherorder(addtwointegers(7), 8);
cout << answer << endl;
}