vault backup: 2022-07-06 20:53:32

Affected files:
02. PARA/03. Resources(資源)/C++17/Structured binding declaration.md
This commit is contained in:
2022-07-06 20:53:32 +08:00
parent b9da824cf9
commit 7c740e0e7a

View File

@@ -35,3 +35,23 @@ auto& [num1, num2, num3] = my_vec;
float rect[4]{ 5.0f, 6.0f, 120.0f, 200.0f }; float rect[4]{ 5.0f, 6.0f, 120.0f, 200.0f };
auto& [x, y, w, h] = rect; auto& [x, y, w, h] = rect;
``` ```
但是不能用來展開 `std::vector`
## 展開 pair
```cpp
std::pair<std::string, int32_t> name_phone{ "John", 912345678 };
auto& [name, phone_number] = name_phone;
```
用在 for-loop 裡也比較好懂,假設我們有一個 vector 用來存剛剛的姓名跟電話:
```cpp
std::vector<std::pair<std::string, uint32_t>> phoneBook = {
{ "John", 912345678 },
{ "Andy", 912345679 },
};
for (const auto& [name, phone] : phoneBook) {
std::cout << "Name: " << name << ", phone: " << phone << std::endl;
}
```