How does the erase-remove idiom work with ranges/constrained algorithms?
Another option would be decomposing the subrange returned by std::ranges::remove
/unique
, and use those iterators:
auto [Beg, End] = std::ranges::remove(v, 42);
v.erase(Beg, End);
It doesn't work since std::ranges::remove()
returns not iterator but range. But even if you try v.erase(std::ranges::remove(...))
it will not work, because vector does not have erase()
overload which takes range as parameter.
Instead, take a look at std::erase()
(defined in <vector>
). What you need is probably just std::erase(v, 42)
.
std::ranges::unique
(and std::ranges::remove
) returns a sub range from the first removed element to the end of the container so you need to use std::begin
before passing to std::vector::erase
:
v.erase(std::ranges::begin(std::ranges::remove(v, 42)), std::end(v));
v.erase(std::ranges::begin(std::ranges::unique(v)), std::end(v));