Vector Access in C++

Level 260 keystrokes

Reads the element at index 1 of the vector {10, 20, 30} with at() and prints 20; at() checks that the index is valid.

C++ code

// Creates a vector with the three values 10, 20 and 30
1vector<int> nums = {10, 20, 30};
// at(1) returns the second element, 20 (indexes start at 0)
// Unlike [], at() throws out_of_range for an invalid index
2cout << nums.at(1) << endl;

Output

20
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.

  • <× 5
  • ,× 2
  • ;× 2
  • .× 1
  • (× 1
  • )× 1
  • {× 1
  • }× 1
  • =× 1
  • >× 1

More C++ examples at level 2

← C++ typing practice