Vector Front and Back in C++

Level 291 keystrokes

Uses front() and back() to read the first and last elements of the vector {10, 20, 30}, printing 10 and 30.

C++ code

// Creates a vector with three elements
1vector<int> nums = {10, 20, 30};
// front() returns the first element (same as nums[0]), so 10 is printed
2cout << nums.front() << endl;
// back() returns the last element, so 30 is printed
// Neither may be called on an empty vector
3cout << nums.back() << endl;

Output

10
30
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.

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

More C++ examples at level 2

← C++ typing practice