STL 删除没有按预期工作?

STL remove doesn#39;t work as expected?(STL 删除没有按预期工作?)

本文介绍了STL 删除没有按预期工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main()
{

        const int SIZE = 10;
        int a[SIZE] = {10, 2, 35, 5, 10, 26, 67, 2, 5, 10};
        std::ostream_iterator< int > output(cout, " ");
        std::vector< int > v(a, a + SIZE);
        std::vector< int >::iterator newLastElement;

        cout << "contents of the vector: ";
        std::copy(v.begin(), v.end(), output);

        newLastElement = std::remove(v.begin(), v.end(), 10);
        cout << "
contents of the vector after remove: ";
        //std::copy(v.begin(), newLastElement, output); 
                         //this gives the correct result : 2 35 5 26 67 2 5
        std::copy(v.begin(), v.end(), output);
          //this gives a 10 which was supposed to be removed : 2 35 5 26 67 2 5 2 5 10

        cout << endl;
        return 0;
}

数组a中有3个10.

为什么我们用remove函数删除了所有的10之后数组v包含一个10.

why does the array v contains a 10 after we remove the all the 10s with remove function.

你也可以在这里

推荐答案

实际上 std::remove 不会从容器中删除项目.引用自此处

Actually std::remove doesn't remove the item from the container. Quoted from here

Remove 从范围 [first, last) 中删除所有等于 value 的元素.也就是说,remove 返回一个迭代器 new_last,使得范围 [first, new_last) 不包含等于 value 的元素.[new_last, last) 范围内的迭代器都仍然可解引用,但它们指向的元素未指定. remove 是稳定的,意味着不等于 value 的元素的相对顺序是不变的.`

Remove removes from the range [first, last) all elements that are equal to value. That is, remove returns an iterator new_last such that the range [first, new_last) contains no elements equal to value. The iterators in the range [new_last, last) are all still dereferenceable, but the elements that they point to are unspecified. Remove is stable, meaning that the relative order of elements that are not equal to value is unchanged.`

也就是说,std::remove 仅适用于一对迭代器,并且对实际包含项目的容器一无所知.实际上,std::remove 不可能知道底层容器,因为它无法从一对迭代器中发现迭代器所属的容器.所以std::remove 并没有真正删除项目,只是因为它不能.实际从容器中删除项目的唯一方法是调用该容器上的成员函数.

That is, std::remove works with a pair of iterators only and does not know anything about the container which actually contains the items. In fact, it's not possible for std::remove to know the underlying container, because there is no way it can go from a pair of iterators to discover about the container to which the iterators belong. So std::remove doesn't really remove the items, simply because it cannot. The only way to actually remove an item from a container is to invoke a member function on that container.

因此,如果您想删除这些项目,请使用 Erase-Remove成语:

So if you want to remove the items, then use Erase-Remove Idiom:

 v.erase(std::remove(v.begin(), v.end(), 10), v.end()); 

<小时>

erase-remove idiom 是如此常见和有用是 std::list 添加了另一个名为 list::remove 的成员函数,它产生与 erase-remove 习语相同的效果.


The erase-remove idiom is so common and useful is that std::list has added another member function called list::remove which produces the same effect as that of the erase-remove idiom.

 std::list<int> l;
 //...
 l.remove(10); //it "actually" removes all elements with value 10!

这意味着,当您使用 std::list 时,您不需要使用 erase-remove 习惯用法.可以直接调用它的成员函数list::remove.

That means, you don't need to use erase-remove idiom when you work with std::list. You can directly call its member function list::remove.

这篇关于STL 删除没有按预期工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:STL 删除没有按预期工作?

基础教程推荐