1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| // 数组绑定
int arr[] = {1, 2, 3};
auto [x, y, z] = arr;
// 元组绑定
std::tuple<int, std::string, double> tuple{1, "hello", 3.14};
auto [id, name, value] = tuple;
// 结构体绑定
struct Point {
int x;
int y;
};
Point p{1, 2};
auto [px, py] = p;
// 在循环中使用
std::map<std::string, int> scores;
for (const auto& [name, score] : scores) {
std::cout << name << ": " << score << "\n";
}
|