June 2017
Intermediate to advanced
532 pages
12h 59m
English
A lot of fundamental data structures from the STL are immediately accessible using structured bindings without us having to change anything. Consider, for example, a loop that prints all the items of an std::map:
std::map<std::string, size_t> animal_population { {"humans", 7000000000}, {"chickens", 17863376000}, {"camels", 24246291}, {"sheep", 1086881528}, /* … */};for (const auto &[species, count] : animal_population) { std::cout << "There are " << count << " " << species << " on this planet.\n";}
This particular example works because when we iterate over an std::map container, we get the std::pair<const key_type, value_type> nodes on every iteration step. Exactly these nodes are unpacked using the structured bindings ...
Read now
Unlock full access