Is there a better alternative to std::remove_if to remove elements from a vector?
Asked Answered
E

2

30

The task of removing elements with a certain property from a std::vector or other container lends itself to a functional style implementation: Why bother with loops, memory deallocation and moving data around correctly?

However the standard way of doing this in C++ seems to be the following idiom:

std::vector<int> ints;
...
ints.erase(
    std::remove_if(ints.begin(), 
                   ints.end(),
                   [](int x){return x < 0;}),
    ints.end());

This example removes all elements less than zero from an integer vector.

I find it not only ugly but also easy to use incorrectly. It is clear that std::remove_if cannot change the size of the vector (as its name would suggest) because it only gets iterators passed. But many developers, including myself, don't get that in the beginning.

So is there a safer and hopefully more elegant way to achieve this? If not, why?

Electrosurgery answered 3/4, 2016 at 10:55 Comment(7)
As always, if it gets complex, wrap it in a (template) function.Ferne
@KarolyHorvath: Yes, but this is such a common task. I don't want to write my own function for it. This belongs into the standard library, just like in other languages.Electrosurgery
Yeah, sure, pray or write a proposal and wait a couple of years. facepalm.Ferne
@KarolyHorvath: Well, bringing up this issue on stack overflow might be a tiny first step. Better than praying I'd say.Electrosurgery
@KarolyHorvath it's coming in C++17, see my answer.Moolah
Boost.Range gives you remove_erase_if(ints, [](int x){return x < 0;}); (since 2010.)Trejo
Would it be nice to add discard_space() or remove_space() to the string class in C++? This will save the collective C++ programer 100 years of time (wasted).Glycoside
P
27

I find it not only ugly but also easy to use incorrectly.

Don't worry, we all did at the start.

It is clear that std::remove_if cannot change the size of the vector (as its name would suggest) because it only gets iterators passed. But many developers, including myself, don't get that in the beginning.

Same. It confuses everyone. It probably shouldn't have been called remove_if all those years ago. Hindsight, eh?

So is there a safer and hopefully more elegant way to achieve this?

No

If not, why?

Because this is the safest, most elegant way that preserves performance when deleting items from a container in which deleting an item invalidates iterators.

anticipating:

Anything I can do?

Yes, wrap this idiom into a function

template<class Container, class F>
auto erase_where(Container& c, F&& f)
{
    return c.erase(std::remove_if(c.begin(), 
                                  c.end(),
                                  std::forward<F>(f)),
                   c.end());    
}

The call in the motivating example then becomes:

auto is_negative = [](int x){return x < 0;};
erase_where(ints, is_negative);

or

erase_where(ints, [](int x){return x < 0;});
Pibgorn answered 3/4, 2016 at 10:59 Comment(5)
Not remove_erase_if? ;)Almsgiver
@Yakk _if is so last year. _where is the new black.Pibgorn
@Yakk coming in LibFun2 en.cppreference.com/w/cpp/experimental/vector/erase_ifMoolah
auto erase_where compile error 'error: 'erase_where' function uses 'auto' type specifier without trailing return type' so I change it to void method.Sannyasi
@Sannyasi or change it to decltype(auto). Which compiler are you using?Pibgorn
M
19

This will become available in a C++17-ready compiler soon through the std::experimental::erase_if algorithm:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include <experimental/vector>

int main()
{
    std::vector<int> ints { -1, 0, 1 };   
    std::experimental::erase_if(ints, [](int x){
        return x < 0;
    });
    std::copy(ints.begin(), ints.end(), std::ostream_iterator<int>(std::cout, ","));
}

Live Example that prints 0,1

Moolah answered 5/4, 2016 at 18:26 Comment(2)
std::erase_if is there in C++20, defined in <vector>.Suggestion
@LevLeontev, but it doesn't allow to use iterator begin, end, so for the time being we're still stuck with remove_if or writing out the loop.Jacklyn

© 2022 - 2024 — McMap. All rights reserved.