On Mon, Apr 6, 2026 at 1:28 PM Adrian Johnston via Std-Proposals <std-proposals@lists.isocpp.org> wrote:

If your array is unordered then you can just swap the end element down when erasing an element.

From my own code:


template<hxarray_concept_ T_, size_t capacity_>

void hxarray<T_, capacity_>::erase_unordered(const T_* pos_) {

    hxassertmsg(pos_ >= this->data() && pos_ < m_end_, "invalid_iterator");

    if(pos_ != --m_end_) {

        *const_cast<T_*>(pos_) = hxmove(*m_end_);

    }

    m_end_->~T_();

}


This is something I think the std::vector could definitely use.


This is properly an algorithm: `stdx::unstable_remove{,_if}`.
See
Brent Friedman, 2015: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0041r0.html
Brent Friedman, 2016: https://iterator.wordpress.com/2016/01/31/algorithms_0/ (archived)
Arthur O'Dwyer, 2023-ish: https://github.com/Quuxplusone/SG14?tab=readme-ov-file#efficient-removal-algorithms-future--c14
Ted Lyngmo, 2025: https://discourse.bemanproject.org/t/help-getting-started-writing-a-propsal-for-unstable-remove-and-friends-wanted/335

Note that David Sankel asked (in the last link above) how std::unstable_remove_if was different from std::partition. The answer is that std::partition swaps; std::unstable_remove_if leaves the right-hand elements in a moved-from state (à la std::shift_left).
Note that (AIUI) Ted Lyngmo abandoned his proposal because the benchmark numbers weren't good enough to justify the standardization effort; see here.

HTH,
Arthur