Structured binding in C++

Level 3121 keystrokes

Structured bindings (C++17) unpack each map entry into two names, key and val, instead of writing p.first and p.second. The map is ordered by key, so it prints a:1 b:2.

C++ code

1map<string, int> m = {{"a", 1}, {"b", 2}};
// [key, val] binds the pair's first and second; & avoids copying the entry
2for (auto& [key, val] : m) {
// Prints a:1 b:2 in key order
3 cout << key << ":" << val << " ";
4}
// Ends the line after the loop
5cout << endl;

Output

a:1 b:2 
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 11
  • "× 8
  • ,× 5
  • {× 4
  • }× 4
  • ;× 3
  • :× 2
  • (× 1
  • )× 1
  • [× 1

More C++ examples at level 3

← C++ typing practice