Structured Bindings in C++17, 8 Years Later -- Bartlomiej Filipek
Structured binding is a C++17 feature that allows you to bind multiple variables to the elements of a structured object, such as a tuple or struct. This can make your code more concise and easier to read, especially when working with complex data structures. On this blog, we already covered this functionality, but we’ll talk about some good C++26 additions and real code use cases.
Structured bindings in C++17, 8 years later
by Bartlomiej Filipek
From the article:
An excellent demonstration of structured bindings is an iteration through a map object.
If you have a
std::map
of elements, you might know that internally, they are stored as pairs of<const Key, ValueType>
.Now, when you iterate through elements, you can do:
for (const auto& elem : myMap) { ... }
You need to write
elem.first
andelem.second
to refer to the key and the value.std::map<KeyType, ValueType> myMap = getMap(); // C++14: for (const auto& elem : myMap) { // elem.first - is the key // elem.second - is the value }