Structured bindings (C++17) cho phép unpack tuple, pair, array, hoặc struct vào các biến riêng biệt với cú pháp ngắn gọn.
cpp
// Trước C++17:
int idx = p.first;
std::string name = p.second;
// C++17 — ngắn và rõ:
auto [idx, name] = std::make_pair(1, "hello");
// Map iteration — rất phổ biến trong interview:
for (const auto& [name, score] : scores) {
std::cout << name << ": " << score << "\n";
}
// Unpack insert result:
auto [it, inserted] = myMap.insert({"key", 42});
if (!inserted) { /* key đã tồn tại */ }Structured bindings (C++17) allow unpacking a tuple, pair, array, or struct into individual named variables with clean syntax.
cpp
// Pre-C++17:
int idx = p.first;
std::string name = p.second;
// C++17 — concise:
auto [idx, name] = std::make_pair(1, "hello");
// Map iteration — very common in interviews:
for (const auto& [name, score] : scores) {
std::cout << name << ": " << score << "\n";
}
// Unpack insert result:
auto [it, inserted] = myMap.insert({"key", 42});
if (!inserted) { /* key already existed */ }