C++ features by examples
Collaboration diagram for Structured bindings:

Functions

void structured_bindings ()
 

Detailed Description

nn

structured binding

Function Documentation

◆ structured_bindings()

void structured_bindings ( )

Definition at line 312 of file 17.cpp.

313{
314
315 int a[2] = {1, 2};
316
317 auto [a0, a1] = a;
318 // a0_ref = &a; a0_ref = &a;
319 auto& [a0_ref, a1_ref] = a;
320
321 float x{};
322 char y{};
323 int z{};
324
325 unordered_map<string, int> mapping {
326 {"a", 1},
327 {"b", 2},
328 {"c", 3},
329 };
330
331 for (const auto& [key, value] : mapping) {
332 // Do something with key and value
333 }
334 //tuple<float&,char&&,int> tpl(x,move(y),z);
335 //const auto& [a,b,c] = tpl;
336 // https://en.cppreference.com/w/cpp/language/structured_binding
337 auto [min, max] = minmax({3, 2, 1});
338 assert(min == 1);
339 assert(max == 3);
340}