Back
Close

Modern C++ idoms and recipes

meshell
19.5K views

Structured Bindings

Structured bindings is a new C++ language feature shipping with C++17. It gives us the ability to declare multiple variables initialized from a tuple, pair or struct.

Example

With C++11/14 you could use std::tie to bind the elements of a pair or tuple to variables:

const auto tuple = std::make_tuple(1, 'a', 2.3);
//...

// first declare the variables
int i;
char c;
double d;

// now we can unpack the tuple
std::tie(i, c, d) = tuple;

With C++17 this becomes much easier:

const auto tuple = std::make_tuple(1, 'a', 2.3);
// ...
const auto [ i, c, d ] = tuple;

DIY

Refactor the code to use structured bindings when ever possible
#include <map>
#include <string>
#include <iostream>
using namespace std::string_literals;
struct address_t {
std::string street;
unsigned plz;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants