A Small Trick That Turns O(n) Vector Erase Into O(1)
A Small Trick That Turns O(n) Vector Erase Into O(1) In day-to-day C++ work, std::vector is the default container. It is fast, cache-friendly, and good enough for almost everything. Except for one thing: removing elements from the middle. That operation is always O(n) because the vector has to shift all subsequent elements to fill the gap. Every time I erase from the middle, I feel the same irritation: a tiny change, half of the array shuffled. ...