Structured binding is C++ declaration syntax for naming elements of pairs, tuples, arrays, and structures. Write auto followed by names in brackets, such as auto [x, y, z] = three_numbers;. section 7.1.2
Use structured bindings to make element access clearer:
- Replace repeated std::get calls when unpacking a tuple. section 7.1.2
- Name map entries directly in a range-based loop, often as const references to avoid copying strings. section 7.1.3
- Choose value or reference bindings deliberately: auto [x, y, z] copies the hidden tuple or pair, while auto& [x, y, z] binds to the existing object. section 7.1.2
Iterating a dictionary
A map loop can expose each key and definition directly: for (const auto& [word, definition] : keywords) { ... }. The same pattern appears in an answer-smash game, where each keyword is paired with an overlapping dictionary word and its offset: auto [second_word, offset] = find_overlapping_word(word, dictionary);. section 7.1.3
Structured bindings also name tuple references produced by std::views::zip, letting a loop move corresponding items from two collections without joining or copying the collections. section 9.3.5
