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

六月ゝ 毕业季﹏ 提交于 2019-11-28 08:58:31

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;});

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!