tuple in C++

Level 4221 keystrokes

Stores a string, an int and a double in one tuple, reads them with get<index>, then unpacks them with a structured binding.

C++ code

// A tuple holds a fixed number of values of different types
1tuple<string, int, double> person("Alice", 25, 5.5);
// get<0> returns the first element: Alice
2cout << get<0>(person) << endl;
// The index is a compile-time constant: 25
3cout << get<1>(person) << endl;
// Third element: 5.5
4cout << get<2>(person) << endl;
5
// Structured binding copies the three elements into named variables
6auto [name, age, height] = person;
// Prints Alice, 25
7cout << name << ", " << age << endl;

Output

Alice
25
5.5
Alice, 25
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.

  • <× 24
  • ,× 7
  • ;× 6
  • "× 4
  • (× 4
  • )× 4
  • >× 4
  • .× 1
  • [× 1
  • ]× 1

More C++ examples at level 4

← C++ typing practice