Vector erase in C++

Level 3108 keystrokes

erase removes the element at an iterator position. v.begin() + 2 points to index 2 (the value 3); the following elements move forward and 1 2 4 5 is printed.

C++ code

1vector<int> v = {1, 2, 3, 4, 5};
// Removes index 2 (value 3); later elements shift left and the size becomes 4
2v.erase(v.begin() + 2);
// Prints the remaining elements: 1 2 4 5
3for (int n : v) {
4 cout << n << " ";
5}
// Ends the line after the loop
6cout << endl;

Output

1 2 4 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.

  • <× 7
  • ,× 4
  • ;× 4
  • (× 3
  • )× 3
  • .× 2
  • "× 2
  • {× 2
  • }× 2
  • :× 1

More C++ examples at level 3

← C++ typing practice