C++ STL移除不';你不能按预期工作吗? intmain() { 常数int SIZE=10; inta[SIZE]={10,2,35,5,10,26,67,2,5,10}; std::ostream_迭代器输出(cout,“”); 标准:向量v(a,a+大小); std::vector::迭代器newLastElement; 原因是STL算法不会修改序列的大小。remove,而不是实际删除项,移动它们并将迭代器返回到“new”端。然后可以将迭代器传递到容器的erase成员函数以实际执行删除: 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 << "\ncontents 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; }
顺便说一下,这就是所谓的“删除-删除成语”C++ STL移除不';你不能按预期工作吗? intmain() { 常数int SIZE=10; inta[SIZE]={10,2,35,5,10,26,67,2,5,10}; std::ostream_迭代器输出(cout,“”); 标准:向量v(a,a+大小); std::vector::迭代器newLastElement; 原因是STL算法不会修改序列的大小。remove,而不是实际删除项,移动它们并将迭代器返回到“new”端。然后可以将迭代器传递到容器的erase成员函数以实际执行删除: 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 << "\ncontents 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; },c++,stl,C++,Stl,顺便说一下,这就是所谓的“删除-删除成语” 编辑:我错了。请参阅注释和Nawaz的回答。因为std::remove实际上并没有收缩容器,它只是将所有元素向下移动以填充“removed”所使用的位置元素。例如,如果您有一个序列12345,并使用std::remove删除值2,则您的序列将看起来像12345。如果您随后删除值4,您将得到1355。在任何时候都不会被告知该序列更短。实际上 std::remove不从容器中删除项目。引用自 Remove从范围[first,last)中删除所有等于值的元素
编辑:我错了。请参阅注释和Nawaz的回答。因为
std::remove
实际上并没有收缩容器,它只是将所有元素向下移动以填充“removed”所使用的位置元素。例如,如果您有一个序列12345
,并使用std::remove
删除值2
,则您的序列将看起来像12345
。如果您随后删除值4
,您将得到1355
。在任何时候都不会被告知该序列更短。实际上 std::remove
不从容器中删除项目。引用自
Remove从范围[first,last)
中删除所有等于值的元素。也就是说,Remove返回一个迭代器new\u last
,这样范围[first,new\u last)
中不包含任何等于值的元素。范围中的迭代器[new_last,last)
仍然是可取消引用的,但它们指向的元素是未指定的。Remove是稳定的,这意味着不等于value的元素的相对顺序不变`
也就是说,std::remove
只与一对迭代器一起工作,不知道实际包含项目的容器的任何信息。事实上,std::remove
不可能知道底层容器,因为它无法从一对迭代器中发现项目所在的容器迭代器属于。因此std::remove
不会真正删除项,只是因为它不能从容器中删除项。从容器中实际删除项的唯一方法是调用该容器上的成员函数
因此,如果要删除项目,请使用:
它非常常见和有用,因为std::list
添加了另一个名为list::remove
的成员函数,该函数产生与erase remove
习惯用法相同的效果
v.erase(std::remove(v.begin(), v.end(), 10), v.end());
std::list l;
//...
l、 remove(10);//它“实际”删除所有值为10的元素!
这意味着,当你使用std::list
时,你不需要使用erase remove
习惯用法。你可以直接调用它的成员函数list::remove
的答案是,经过新端点的元素是未指定的,不一定等于删除的元素,因此你看到的是一个particu的工件lar实现。重命名函数不合适。std::remove
不需要像您所想的那样将匹配值移动到序列的末尾。顺便问一下,这是注释(//这给出了正确的结果:2 35 5 26 67 2 5 2 5
)程序真正发出的是什么?你真的想说235 5 26 67 2 5
,对吗?是的,绝对正确。感谢你指出,cplusplus.com
似乎已经更正了所有定义,请查看并编辑你的答案,因为它对该网站造成负面影响。
v.erase(std::remove(v.begin(), v.end(), 10), v.end());
std::list<int> l;
//...
l.remove(10); //it "actually" removes all elements with value 10!