Tsallaka zuwa abun ciki
Justice Tech School

Structured Bindings

Unpack pairs, tuples and structs into named variables.

Example
#include <iostream>
#include <map>
#include <string>
#include <utility>

int main() {
  std::pair<std::string, int> p{"Ada", 30};
  auto [name, age] = p;
  std::cout << name << " " << age << "\n";
  std::map<std::string, int> m{{"a", 1}, {"b", 2}};
  for (auto &[k, v] : m) std::cout << k << "=" << v << " ";
  std::cout << "\n";
  return 0;
}

Result

Ada 30
a=1 b=2 

Exercise

Unpack.

auto [a, ] = p;

Quiz Test

Which standard added them?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.