Structured bindings
Unpack a pair, tuple, struct, or array into named variables in one declaration.
auto [a, b] = ... destructures aggregates into named locals, so map iteration, multiple returns, and tuple access read like plain variables instead of .first/std::get.
A structured binding (C++17) introduces several named variables from a single object in one declaration: auto [a, b] = expr;. The compiler matches the names positionally against the elements of the right-hand side, so you work with a and b directly instead of .first, .second, or std::get<N>.
It works on three kinds of right-hand side: a std::pair or std::tuple (matched by std::get), a plain struct or array with public members (matched in declaration order), and anything that opts into the tuple protocol. The number of names in the brackets must exactly match the number of elements.
Binding modes and references
auto [a, b] = x copies x first, then binds names to the copy. Use auto& [a, b] = x to bind to the original object β essential when iterating a container so you can mutate elements in place, and const auto& [a, b] for cheap read-only access without copies. This is what makes for (auto& [k, v] : map) both concise and efficient.
Where it shines
The two everyday wins are iterating associative containers β for (const auto& [key, value] : m) instead of it->first / it->second β and unpacking a function that returns a tuple or pair, so a "multiple return values" function reads naturally at the call site.
Try it 5 examples
Unpack a std::pair
C++introPlain auto [name, qty] copies the pair, so qty += 10 makes local qty now 15 while entry.second stays 5 β proof the bound names are independent locals, not aliases into the original object.