-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy path2.05.structured.binding.cpp
More file actions
31 lines (27 loc) 路 808 Bytes
/
Copy path2.05.structured.binding.cpp
File metadata and controls
31 lines (27 loc) 路 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//
// 2.5.structured.binding.cpp
// chapter 2 language usability
// modern cpp tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//
#include <iostream>
#include <tuple>
#include <string>
#include <map>
std::tuple<int, double, std::string> f() {
return std::make_tuple(1, 2.3, "456");
}
int main() {
// Unpack a tuple straight into named variables.
auto [x, y, z] = f();
std::cout << x << ", " << y << ", " << z << std::endl;
// Bind each key/value pair while iterating an associative container,
// instead of writing it->first / it->second.
std::map<std::string, int> scores{{"alice", 90}, {"bob", 80}};
for (const auto& [name, score] : scores) {
std::cout << name << ": " << score << '\n';
}
return 0;
}