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

December 3, 2025

EnumArray - A Practical Mapping Trick Without Paying Hash-Map Tax

EnumArray - A Practical Mapping Trick Without Paying Hash-Map Tax Whenever I work with configuration systems, metadata tables, or low-level processing pipelines, I repeatedly run into the same pattern: I have an enum, and I want a dictionary that maps each enumerator to some piece of data. The naïve solution is universal and convenient: 1 2 3 4 5 6 std::unordered_map<Unit, const char*> unitNames { { Unit::Grams, "g" }, { Unit::Meters, "m" }, { Unit::Liters, "l" }, { Unit::Items, "pcs" }, }; It works. It is readable. It is also wasteful. ...

December 1, 2025