Issue with C++ remove_if causing data duplication
仅提供English版本
When using remove_if, I found that this function cannot delete data in the container that satisfies the pred expression. remove_if will use other data in the list to fill, while the size itself remains unchanged. For specific explanation, refer to Usage of remove_if. In this case, you need to use the erase method to completely delete it.
1 | bool IsMoreThen(LinkList target){return target.count>=5;} |
Reason
The remove_if method finds elements that meet the condition and uses erase to delete the returned vector<_type_>::iterator value, and jumps to the next element. At this time, because the element pointer automatically jumps +1, it will cause missed deletions in continuous values. For this situation, you need to write a method to recalculate.
1 | std::vector<LinkList> RemovePredList(std::vector<LinkList> lists){ |
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.





