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
2
bool IsMoreThen(LinkList target){return target.count>=5;}
list.erase(std::remove_if(list.begin(), list.end(), IsMoreThen));

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
2
3
4
5
6
7
8
9
10
11
std::vector<LinkList> RemovePredList(std::vector<LinkList> lists){
std::vector<LinkList>::iterator iter = lists.begin();
while (iter!=lists.end()){
if(iter->count>=5)
iter = lists.erase(iter);
else
iter++;
}
return lists;
}