Files
Obsidian-Main/02. PARA/03. Resources(資源)/C++17/lambda.md
Awin Huang 352834243a vault backup: 2022-06-13 15:30:30
Affected files:
02. PARA/03. Resources(資源)/C++17/lambda.md
2022-06-13 15:30:30 +08:00

36 lines
1021 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
tags:
aliases:
date: 2022-06-12
time: 18:21:42
description:
---
一個簡單的 Lamdba 運算式:
```cpp
[] (int x, int y) -> bool {
return x < y;
}
```
- 以中括號開頭,中括號被稱為*lamdba 導入器lamdba introducer*
- 小括號裡面是*lamdba 參數列表lambda parameter list*
- 如果沒有參數,小括號可以省略,`[] () {...}` 可以簡寫成 `[] {...}`
- 箭號(`->`)後面是回傳的型別,如果沒寫就由 `return` 自動推斷
將 Lamdba 運算式指定給變數:
```cpp
auto comapre = [] (int x, int y) -> bool {
return x < y;
};
```
## Lamdba的擷取子句
以中括號開頭的 *lamdba 導入器*可以將外部的變數傳給 Lamdba 運算式正式名稱是「擷取子句capture clause」。
[=] 表示它們會以值擷取captured by value
[&] 表示它們會以址擷取captured by reference
### 以值擷取captured by value
假設有一個function如下
### 以址擷取captured by reference