C++ Logo

std-proposals

Advanced search

[std-proposals] Proposal draft: unstable erase operations for std::vector

From: 佟晓晴 <bfzgsz_at_[hidden]>
Date: Sat, 23 May 2026 10:20:36 +0800
Hello,


I would like to propose a small standard library extension for std::vector:
std::unstable_erase and std::unstable_erase_if.


The motivation is to standardize the common swap-and-pop idiom used when
removing elements from a vector where the relative order of the remaining
elements is not semantically important.


C++20 provides std::erase and std::erase_if for standard containers. For
std::vector, these operations preserve the relative order of the remaining
elements. In performance-sensitive code, such as games, simulations, servers,
and dense object storage, preserving order can impose unnecessary relocation
costs.


The proposed interface is:


&nbsp; &nbsp; template<class T, class Alloc, class U&gt;
&nbsp; &nbsp; constexpr typename std::vector<T, Alloc&gt;::size_type
&nbsp; &nbsp; unstable_erase(std::vector<T, Alloc&gt;&amp; c, const U&amp; value);


&nbsp; &nbsp; template<class T, class Alloc, class Pred&gt;
&nbsp; &nbsp; constexpr typename std::vector<T, Alloc&gt;::size_type
&nbsp; &nbsp; unstable_erase_if(std::vector<T, Alloc&gt;&amp; c, Pred pred);


The operation removes matching elements but does not preserve the order of
the remaining elements. It is intended as an unstable counterpart to
std::erase / std::erase_if for std::vector, not as a replacement for them.


A public draft paper, prototype implementation, and preliminary benchmark
results are available here:


&nbsp; &nbsp; https://github.com/BaiJin0224/unstable-erase


I would appreciate feedback on the following points:


1. Whether this is a reasonable container-level facility for std::vector.
2. Whether the names unstable_erase and unstable_erase_if are appropriate.
3. Whether the initial scope should remain limited to std::vector.
4. Whether the wording should specify a swap-and-pop-like complexity model.
5. How exception safety should be specified.


Thank you,
Xisheng Liu

Received on 2026-05-23 02:20:46