How to Erase Element from Vector in C++: Syntax and Examples
To erase an element from a
std::vector in C++, use the erase() method with an iterator pointing to the element you want to remove. You can erase a single element or a range of elements by passing iterators to erase().Syntax
The erase() method removes elements from a std::vector. It takes either a single iterator or a range of iterators.
vec.erase(pos);removes the element at the positionpos.vec.erase(first, last);removes all elements in the range fromfirstup to but not includinglast.
After erasing, the vector shifts elements to fill the gap and reduces its size.
cpp
vec.erase(iterator position); vec.erase(iterator first, iterator last);
Example
This example shows how to erase a single element and a range of elements from a vector.
cpp
#include <iostream> #include <vector> int main() { std::vector<int> vec = {10, 20, 30, 40, 50}; // Erase the element at index 2 (value 30) vec.erase(vec.begin() + 2); // Erase elements from index 1 to 3 (values 20 and 40) vec.erase(vec.begin() + 1, vec.begin() + 3); // Print remaining elements for (int val : vec) { std::cout << val << " "; } return 0; }
Output
10 50
Common Pitfalls
Common mistakes when erasing elements from a vector include:
- Using an invalid iterator after erasing, since
erase()invalidates iterators at and after the erased position. - Trying to erase elements by value directly (like
vec.erase(30);) which is incorrect; you must use iterators. - Not updating iterators when erasing elements inside a loop, which can cause skipping elements or crashes.
cpp
#include <vector> #include <iostream> #include <algorithm> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; // Wrong: trying to erase by value (won't compile) // vec.erase(3); // Correct: find iterator to element with value 3 and erase auto it = std::find(vec.begin(), vec.end(), 3); if (it != vec.end()) { vec.erase(it); } // Erasing inside a loop - wrong way (skips elements) // for (auto it = vec.begin(); it != vec.end(); ++it) { // if (*it % 2 == 0) { // vec.erase(it); // iterator invalidated // } // } // Correct way: use iterator returned by erase for (auto it = vec.begin(); it != vec.end(); ) { if (*it % 2 == 0) { it = vec.erase(it); // erase returns next valid iterator } else { ++it; } } for (int v : vec) std::cout << v << " "; return 0; }
Output
1 3 5
Quick Reference
Remember these tips when erasing elements from a vector:
- Use
erase()with iterators, not values. - Erasing invalidates iterators at and after the erased position.
- When erasing in loops, update the iterator with the return value of
erase(). - To remove elements by value, combine
std::removeanderase()(the erase-remove idiom).
Key Takeaways
Use vector's erase() method with iterators to remove elements safely.
Erase invalidates iterators at and after the erased position; update iterators accordingly.
To erase elements by value, find their iterator first or use the erase-remove idiom.
When erasing inside loops, assign the iterator to the erase() return value to avoid skipping elements.