简体   繁体   English

erase和remove / remove_if算法之间的区别?

[英]difference between erase and remove/remove_if algorithms?

What is really the difference between the algorithms remove and remove_if and the member function erase? 算法remove和remove_if与成员函数erase之间的区别是什么? Does both of them result in a call to the removed objects destructor? 它们是否都会导致调用已删除的对象析构函数?

No, remove and remove_if only move objects around in the sequence. 不, removeremove_if只移动序列中的对象。 You need to call erase to make the sequence actually shorter. 您需要调用erase来使序列实际上更短。 The return value of remove and remove_if is the iterator you can use in an erase call to shorten the sequence: remove和remove_if的返回值是可以在erase调用中使用的迭代器,以缩短序列:

sequence.erase(remove(...),sequence.end());

No, std::remove_if will move the elements that don't match the predicate to the end of list and will return an iterator to the new "end". 不, std::remove_if会将与谓词不匹配的元素移动到列表的末尾,并将迭代器返回到新的“end”。 Erase will effectively drop the element (call the dtor) from the container. 擦除将有效地从容器中删除元素(调用dtor)。

The difference is perfectly illustrated by the examples here and here . 这里这里的例子很好地说明了这种差异。

很简单,std :: remove使用一个值来确定元素是否被“删除”,而std :: remove_if使用谓词函数。

无论您使用何种方法/功能,都会在删除项目时始终调用析构函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM