是否有更好的替代std :: remove_if从向量中删除元素? [英] Is there a better alternative to std::remove_if to remove elements from a vector?

查看:248
本文介绍了是否有更好的替代std :: remove_if从向量中删除元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: vector 或其他容器中删除具有特定属性的元素的任务适用于函数式实现:为什么要烦恼循环,内存释放和



<$>然而,在C ++中这样做的标准方法似乎是以下成语:



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

此示例从整数向量中删除小于零的所有元素。



我发现它不仅丑陋,而且容易使用不正确。很明显, std :: remove_if 不能改变向量的大小(如它的名字所暗示的),因为它只得到迭代器的传递。但是许多开发人员,包括我自己,在开始时都没有。



那么,有一个更安全,希望更优雅的方式来实现这一点?

解决方案


我发现它不仅丑陋,而且容易使用不正确。


不要担心,我们都在开始。


很明显,std :: remove_if不能改变向量的大小(如它的名字所暗示的),因为它只能获得迭代器的传递。但是很多开发者,包括我自己,在开始时都没有。


它混淆了每个人。它可能不应该被称为 remove_if 所有那些年前。 Hindsight,eh?


那么,有一个更安全,希望更优雅的方式来实现吗?




因为这是从容器中删除项目时保留性能的最安全,最优雅的方法,其中删除项目会使迭代器无效。



预期:


我可以做什么吗?


是,将此惯用语包含到函数中

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

激励示例中的调用将变为:

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

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


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?

解决方案

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

这篇关于是否有更好的替代std :: remove_if从向量中删除元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆