Erasing elements in stdvector by using indexes
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Removing items from a std::vector by index is easy for one element and surprisingly easy to get wrong for many elements. The reason is that erase shifts later elements left, so every removal changes the indexes that follow.
Removing One Element by Index
For a single index, use erase with an iterator:
Output:
The bounds check matters. values.begin() + index is undefined behavior if index is past the end.
Removing Multiple Indexes Safely
Suppose you want to remove indexes 1, 3, and 4 from the original vector. If you erase them in ascending order, the later positions no longer refer to the same elements after the first erase.
The safest direct approach is to erase in descending order:
Because the largest index is removed first, earlier indexes stay valid relative to the original layout.
When Many Indexes Need Removal
Repeated erase calls can be expensive because every erase shifts part of the vector. If you are removing many elements, it is often faster to build a keep-mask and compact in one pass.
This approach does not preserve original iterators, but neither does erase. It is often easier to reason about when the removal set is large.
Range Erasure for Contiguous Indexes
If the indexes form one continuous block, erase the whole range at once:
That removes the elements originally at indexes 1, 2, and 3, leaving 10 50.
Range erase is clearer and usually faster than three separate single-element erasures.
Iterator and Reference Invalidation
Any erase from a vector invalidates iterators and references at or after the erased position. That means this pattern is dangerous:
After the erase, it may no longer be valid. If you need to keep working with positions after removals, recompute them from the current vector state.
Common Pitfalls
The biggest mistake is erasing multiple indexes in ascending order. After the first erase, the remaining indexes no longer point to the same elements.
Another problem is skipping bounds checks. Vectors do not protect you from invalid iterator arithmetic.
Developers also forget about iterator invalidation and keep references to elements that have been shifted or destroyed.
Finally, do not assume repeated erase is the best option for large removal sets. Rebuilding the vector can be simpler and more efficient.
Summary
- Use
erase(begin() + index)for a single valid index. - For multiple removals, erase indexes in descending order.
- Use range erase when the indexes are contiguous.
- Expect iterators and references after the erased position to become invalid.
- For many removals, rebuilding the vector in one pass is often cleaner and faster.
Related reading
- Error - The transaction associated with the current connection has completed but has not been disposed
- Error 1022 - Can't write; duplicate key in table
- ERROR 1044 42000 Access denied for user '''localhost' to database 'db
- ERROR 1045 28000 Access denied for user 'root''localhost' using password YES
- Error Microsoft Visual C 14.0 is required Unable to find vcvarsall.bat
- error static assertion failed stdthread arguments must be invocable after conversion to rvalues
- Error 1046 No database selected, how to resolve?
- ERROR 1067 42000 Invalid default value for 'created_at

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.