为什么Vector不提供remove()成员函数而list提供? [英] why Vector doesn't provide the remove() member function while list provides?

查看:470
本文介绍了为什么Vector不提供remove()成员函数而list提供?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我要从向量中删除所有元素,我调用删除算法,然后调用向量的擦除成员函数物理删除它。
但是在list的情况下,简单的调用remove成员函数,它将删除所有具有该值的元素。
我不知道为什么向量不提供删除MF而列表它。

If I want to delete all the elements with a value from vector,I call remove algorithm and then call vector's erase member function to physically delete it. But in the case of list , simple call remove member function and it will delete all elements with that value. I am not sure why vector does't provide the remove MF while list does it.

对于Exp:我想从向量v中删除值'4'。

For Exp: I want to delete value '4' from vector v.

vector<int> v;
vector<int> ::iterator Itr;
for (int i=0; i< 6; i++)
   v.push_back(i*2);
v.push_back(4);
v.push_back(8);
v.push_back(4);
v.erase(remove(v.begin(),v.end(),4), v.end()); 

和列表:

list.remove(4); // will delete all the element which has value 4


推荐答案

问题不是为什么 std :: vector 不提供操作,而是为什么 std :: list 提供它。 STL的设计集中在通过迭代器分离容器和算法,并且在所有情况下,其中算法可以在迭代器方面有效地实现。

The question is not why std::vector does not offer the operation, but rather why does std::list offer it. The design of the STL is focused on the separation of the containers and the algorithms by means of iterators, and in all cases where an algorithm can be implemented efficiently in terms of iterators, that is the option.

然而,有一些情况下,有特定的操作,可以更有效地实现容器的知识。这是从容器中删除元素的情况。使用 remove-erase 习惯的成本在容器的大小上是线性的(不能减少太多),但是隐藏了这样的事实:在最坏的情况下,除了一个操作之外, em (唯一匹配的元素是第一个),这些副本可以代表相当大的隐藏成本。

There are, however, cases where there are specific operations that can be implemented much more efficiently with knowledge of the container. That is the case of removing elements from a container. The cost of using the remove-erase idiom is linear in the size of the container (which cannot be reduced much), but that hides the fact that in the worst case all but one of those operations are copies of the objects (the only element that matches is the first), and those copies can represent quite a big hidden cost.

通过实现操作作为 std :: list 中的方法,操作的复杂性仍然是线性的,但是删除的每个元素的相关成本是非常低,几个指针副本和释放内存中的节点。同时,作为列表一部分的实现可以提供更强的保证:对于未被擦除的元素,指针,引用和迭代器不会在操作中失效。

By implementing the operation as a method in std::list the complexity of the operation will still be linear, but the associated cost for each one of the elements removed is very low, a couple of pointer copies and releasing of a node in memory. At the same time, the implementation as part of the list can offer stronger guarantees: pointers, references and iterators to elements that are not erased do not become invalidated in the operation.

在特定容器中实现的另一个算法示例是 std :: list :: sort ,它使用 mergesort

Another example of an algorithm that is implemented in the specific container is std::list::sort, that uses mergesort that is less efficient than std::sort but does not require random-access iterators.

因此,基本上来说,算法被实现为具有迭代器的自由函数,除非有强烈的理由在混凝土容器中提供特定的实现。

So basically, algorithms are implemented as free functions with iterators unless there is a strong reason to provide a particular implementation in a concrete container.

这篇关于为什么Vector不提供remove()成员函数而list提供?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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