Vector basics in C++

Level 3118 keystrokes

Creates a vector<int> with five elements and prints its size (5), its first element (1) and its last element (5).

C++ code

// vector is a resizable array; the initializer list gives it 5 elements
1vector<int> nums = {1, 2, 3, 4, 5};
// size() returns the number of elements: 5
2cout << nums.size() << endl;
// [] accesses an element by index; indices start at 0, so this prints 1
3cout << nums[0] << endl;
// back() returns the last element: 5
4cout << nums.back() << endl;

Output

5
1
5
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
  • ,× 4
  • ;× 4
  • .× 2
  • (× 2
  • )× 2
  • [× 1
  • ]× 1
  • {× 1
  • }× 1

More C++ examples at level 3

← C++ typing practice