pair type in C++

Level 3153 keystrokes

A pair holds two values, possibly of different types, accessed as first and second. Prints Alice and 25, then changes second to 26 and prints it.

C++ code

// The template arguments give the types of the two values: string and int
1pair<string, int> person = {"Alice", 25};
// first is the first value: Alice
2cout << person.first << endl;
// second is the second value: 25
3cout << person.second << endl;
// The members can be assigned directly
4person.second = 26;
// Prints the new value 26
5cout << person.second << endl;

Output

Alice
25
26
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.

  • <× 13
  • ;× 5
  • .× 4
  • ,× 2
  • "× 2
  • =× 2
  • {× 1
  • }× 1
  • >× 1

More C++ examples at level 3

← C++ typing practice