cpp_lambda - 8BitsCoding/RobotMentor GitHub Wiki
์ด๋ฆ์๋ ํจ์~~
์ธ์ ์ธ์ง ๋ณด์
// ๋ฒกํฐ ์ ๋ ฌํ๊ธฐ 1
#include <algorithm>
#include <vector>
struct Comparer
{
bool operator()(float a, float b)
{
return (a > b);
}
};
int main() {
std::vector<float> scores;
scores.push_back(50.f);
scores.push_back(88.5f);
scores.push_back(70.f);
Comparer comparer;
std::sort(scores.begin(), scores.end(), comparer);
return 0;
}
// ๋ฒกํฐ ์ ๋ ฌํ๊ธฐ 2
#include <algorithm>
#include <vector>
bool Sort(float a, float b)
{
return (a > b);
}
int main() {
std::vector<float> scores;
scores.push_back(50.f);
scores.push_back(88.5f);
scores.push_back(70.f);
Comparer comparer;
std::sort(scores.begin(), scores.end(), Sort);
return 0;
}
๋ณดํต์ ์ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ ์ค ํ๋๋ฅผ ์ฌ์ฉ!
์ฌํ์ฉ ํ์ง ์์ ํจ์๋ฅผ ์์ฑํ๋ ๊ฒ์ด ๋ง๋ ๊ฑด๊ฐ??? -> lambda ๋์ !
// ๋๋ค์ ์์
int main() {
std::vector<float> scores;
scores.push_back(50.f);
scores.push_back(88.5f);
scores.push_back(70.f);
Comparer comparer;
std::sort(scores.begin(), scores.end(), [](float a, float b){return (a > b)}]);
return 0;
}
- ์ด๋ฆ์ด ์๋ ํจ์ ๊ฐ์ฒด
- ๋ดํฌ๋๋ ํจ์
[<captures>](<parameters>) <specifiers> -> <return_type>
{
<body>
}
- captures : ์บก์ฒ ๋ธ๋ก
- parameters : ๋งค๊ฐ๋ณ์ ๋ชฉ๋ก
- specifiers : ์ง์ ์
- return_type : ๋ฐํํ
ํ๋๋ ๋ชจ๋ฅด๊ฒ ๋ค... ์์ธํ ์ค๋ช ์ ์๋์...
- ๋๋ค ์์ ํ์ ๋ฒ์ ์์ ์๋ ๋ณ์๋ฅผ ๋๋ค ์์ ๋๊ฒจ์ค๋ ์ฌ์ฉ
- [] : ๋น์ด ์์, ์บก์ฒํ์ง ์์
- [=] : ๊ฐ์ ์ํ ์บก์ฒ, ๋ชจ๋ ์ธ๋ถ ๋ณ์๋ฅผ ์บก์ฒํ๋ค. (๋จ, ๋๋ค์ ๋ด์์ ์์ ๋ถ๊ฐ)
- [&] : ์ฐธ์กฐ์ ์ํ ์บก์ฒ, ๋ชจ๋ ์ธ๋ถ ๋ณ์๋ฅผ ์บก์ฒ
- [<๋ณ์ ์ด๋ฆ>] : ํน์ ๋ณ์๋ฅผ ๊ฐ์ผ๋ก ์บก์ฒ (์ญ์, ๋๋ค์๋ด์์ ์์ ๋ถ๊ฐ)
- [&<๋ณ์ ์ด๋ฆ>] : ํน์ ๋ณ์๋ฅผ ์ฐธ์กฐ๋ก ์บก์ฒ
// ์บก์ฒ๋ฅผ ํ์ง ์๋ ๊ฒฝ์ฐ []
// ๋ฐฉ๋ฒ 1
auto noCapture = []() { std::cout << "No capture" << std::endl; };
noCapture(); // ํธ์ถ์ ์๋ ๊ฒ
// ๋ฐฉ๋ฒ 2
[]{ std::cout << "No capture2" << std::endl; };
// ๊ฐ์ ์ํ ์บก์ฒ [=]
float score1 = 80.f;
float score2 = 20.f;
auto max = [=]() { return score1 > score2 ? score1 : score2; };
max();
// ์ฐธ์กฐ์ ์ํ ์บก์ฒ [&]
float score1 = 80.f;
float score2 = 20.f;
auto max = [&]() { score1 = 60.f; };
max();
// ํน์ ๋ณ์ ์บก์ฒ [<๋ณ์ ์ด๋ฆ>]
float score1 = 80.f;
float score2 = 20.f;
auto max = [score1]() { return score1 > score2 ? score1 : score2; };
// Compile Error
// ํน์ ๋ณ์ ์ฐธ์กฐ ์บก์ฒ [&<๋ณ์ ์ด๋ฆ>]
float score1 = 80.f;
float score2 = 20.f;
auto max = [&score1]() { score1 = 10.f; };
// Compile Error
// ์บก์ฒ ์ต์
์๊ธฐ
auto changeValue = [=, &score1]() {};
- ์ ํ์ฌํญ์ด๊ณ
- () ์์ฒด๋ฅผ ์๋ต๋ ํ ์ ์๋ค.
int main() {
int score1 = 70;
int score2 = 85;
auto add = [](int a, int b) {
return a+b;
};
std::cout << add(score1, score2) << std::endl;
}
// ์ ๋ ฌํ๊ธฐ์ ๋ฑ ์ ํฉ!
int main() {
std::vector<float> scores;
scores.push_back(50.f);
scores.push_back(88.5f);
scores.push_back(70.f);
Comparer comparer;
std::sort(scores.begin(), scores.end(), [](float a, float b){return (a > b)}]);
return 0;
}
- ์ ํ์ฌํญ
- mutable
- ๊ฐ์ ์ํด ์บก์ฒ๋ ๊ฐ์ฒด๋ฅผ ์์ ํ ์ ์๊ฒํจ
- ๊ด์ฐฎ์ ์ธ์ด ๋์์ธ, ํ๋ C++์ ๋๋ฌด ๋ฆ๊ฒ ๋ค์ด์ด.
int main() {
int value = 100;
auto foo = [value]() mutable {
std::cout << ++value << std::endl;
};
foo();
}
- ์ ํ ์ฌํญ
- ๋ฐํ ํ์ ์ ์ง ์์ผ๋ฉด ๋ฐํ๋ฌธ์ ํตํด ์ ์ถ (๊ฐ๋ ์ฑ์ ํด์น ์ ์๋ค.)
- ํจ์ ์ฌ์ฌ์ฉ์ฑ์ด ๋ฎ๋ค
- ์ฌ๋๋ค์ ๋ณดํต ํจ์๋ฅผ ์๋ก ๋ง๋ค๊ธฐ ์ ์ ํด๋์ค์ ์๋ ๊ธฐ์กดํจ์๋ฅผ ์ฐพ๋๋ค.
- ๋๋ค ํจ์๋ ๋์ ์ ๋์ง ์์์ ๋ชป ์ฐพ์ ๊ฐ๋ฅ์ฑ์ด ๋๊ณ
- ์ฝ๋์ ์ค๋ณต์ ์ผ๊ธฐํ๋ค.
- ๊ธฐ๋ณธ์ ์ผ๋ก ์ด๋ฆ์ด ์๋ ํจ์๋ฅผ ์ ํธ
- ์์ํ ํจ์๋ ๋๋ค์์ผ๋ก ํํ! (ํ ์ค์ง๋ฆฌ ํจ์๋ผ๋ ์ง?)
- ์ ๋ ฌ ํจ์ ์ฒ๋ผ STL ์ปจํ ์ด๋์ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌํ ํจ์๋ค์ด ์ข์ ํ๋ณด!