lambda 함수는 stl과 결합하면 상당히 좋은 성능을 보인다.
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <utility>
#include <execution>
class Cat
{
public:
explicit Cat(int age) : mAge{ age } {};
~Cat() noexcept { std::cout << "Cat destructor finished " << std::endl; };
int age() const
{
return mAge;
};
void speak() const
{
std::cout << "meow" << std::endl;
};
void test() const
{
// class 안에서 &를 붙이면 this를 capture하는 것임(this는 object의 point)
//auto lambda = [&]()
// explicit하게 capture를 넘겨주는게 좋다.
auto lambda = [this] ()
{
std::cout << "lambdaFunction" << std::endl;
std::cout << mAge << std::endl;
// std::cout << this->mAge << std::endl;
speak();
};
lambda();
};
private:
int mAge;
};
int main()
{
Cat kitty{ 1 };
kitty.test();
std::vector<int> nums{ 1,2,3,4,5,6,7,8,9,10 };
auto lambdaAdd10 = [](int& n)
{
n += 10;
};
int n = 10;
lambdaAdd10(n);
std::cout << n << std::endl;
///////////////////////////////////
// for each stl 적용하기 //
///////////////////////////////////
//std::for_each(nums.begin(), nums.end(), lambdaAdd10);
// lambda의 장점 -> unanimous 함수로 가능
std::for_each(nums.begin(), nums.end(), [](int& n)
{
n += 10;
});
for (int num : nums)
{
std::cout << num << " ";
}
std::cout << std::endl;
///////////////////////////////////
// for each stl 적용하기 //
///////////////////////////////////
std::vector<int> nums2{ 1,2,3,4,5,6,7,8,9,10 };
auto filterOdd = [](int n)
{
return n % 2 == 1;
};
//nums2.erase(std::remove_if(nums2.begin(), nums2.end(), filterOdd));
nums2.erase(std::remove_if(nums2.begin(), nums2.end(), [](int n)
{
return n % 2 == 1;
}), nums2.end());
std::cout << "Odd erase" << std::endl;
for (int num: nums2) {
std::cout << num << " ";
}
std::cout << std::endl;
///////////////////////////////////
// sorting 적용하기 //
///////////////////////////////////
std::vector<int> nums3{ 10,5,4,7,9,8,3,2,1 };
std::sort(nums3.begin(), nums3.end(), [](int a, int b)
{
//return a < b;
return std::abs(a - 5) < std::abs(b - 5);
});
std::cout << "sorting" << std::endl;
for (int num : nums3) {
std::cout << num << " ";
}
///////////////////////////////////
// reduce 적용하기 //
///////////////////////////////////
std::vector<int> nums4{ 10,5,4,7,9,8,3,2,1,6 };
int sum = std::reduce(nums4.begin(), nums4.end(), 0, [](int a, int b)
{
return a + b;
});
std::cout << std::endl;
std::cout << "sum reduce" << std::endl;
std::cout << sum << std::endl;
int mul = std::reduce(nums4.begin(), nums4.end(), 1, [](int a, int b)
{
return a*b;
});
std::cout << std::endl;
std::cout << "multiply reduce" << std::endl;
std::cout << mul << std::endl;
///////////////////////////////////
// class에 sort 적용하기 //
///////////////////////////////////
Cat acat{ 10 };
std::cout << "고양이 나이 : " << acat.age() << std::endl;
std::vector<Cat> cats;
for (int i = 100; i > 0; i--)
{
cats.push_back(Cat{ i });
std::cout << "cat " << i << "번째 고양이가 입력되었습니다" << std::endl;
};
std::sort(cats.begin(), cats.end(), [](Cat& a, Cat& b) {
return a.age() < b.age();
}
);
for (Cat cat : cats)
{
std::cout << cat.age() << std::endl;
};
return 0;
}
|
'C' 카테고리의 다른 글
c++ vector basic 설명 (0) | 2021.06.25 |
---|---|
c++ std::function 사용법 (0) | 2021.06.24 |
c++ lambda expression, capture =, & (0) | 2021.06.23 |
c++ weak pointer 정리 (0) | 2021.06.23 |
c++ heap 메모리 생성, 해제, unique_ptr, shared_ptr (0) | 2021.06.23 |