vault backup: 2025-03-04 11:17:00
This commit is contained in:
39
20.02. CPP/C++17.md
Normal file
39
20.02. CPP/C++17.md
Normal file
@@ -0,0 +1,39 @@
|
||||
- 變數宣告的方式變了
|
||||
- Old: `int a = 3;`
|
||||
- New: `int a {3};`
|
||||
|
||||
- `if`裡面可以宣告變數
|
||||
```cpp
|
||||
if (auto a {3}; a > b) {
|
||||
// Do something
|
||||
}
|
||||
```
|
||||
|
||||
- `unique_ptr`: 無法複製的指標
|
||||
- 傳統方法:
|
||||
```cpp
|
||||
unique_ptr<uint8_t[]> buffer = new uint8_t[256];
|
||||
```
|
||||
- 新方法:
|
||||
```cpp
|
||||
auto buffer = std::make_unique<uint8_t[]>(256);
|
||||
```
|
||||
- `share_ptr`: 可以複製,但要避免循環參考問題
|
||||
|
||||
- 透過refernce傳遞array參數
|
||||
- 考慮一個帶有長度的陣列要傳到function裡面,但是又希望在function面可以指定陣列長度
|
||||
```cpp
|
||||
double value[] { 1.0, 2.0, 3.0 }; // Error!
|
||||
double value[] { 1.0, 2.0, 3.0, 4.0, 5.0 }; // Pass!
|
||||
|
||||
double average(const double (&array)[5]) {
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
- 用 `std::string_view` 代替 `const std::string&`。
|
||||
|
||||
## Multi-Thread
|
||||
### 使用`std::async`
|
||||
- [C++ 使用 Async 非同步函數開發平行化計算程式教學](https://blog.gtwang.org/programming/cpp-11-async-function-parallel-computing-tutorial/)
|
||||
- [std::atomic](https://en.cppreference.com/w/cpp/atomic/atomic)
|
||||
Reference in New Issue
Block a user