为什么vector的remove_if实际上并没有删除元素,而要配合erase使用

Erase-Remove idiom是C++中用于从容器中删除满足特定条件元素的常见编程技巧。它不直接删除元素,而是将不符合条件的元素移到容器前面,保持相对顺序,然后通过容器自身的erase成员函数来实际移除元素。这种方法不适用于返回const迭代器的容器,如set。
摘要由CSDN通过智能技术生成

Erase–remove idiom



Motivation[edit]

A common programming task is to remove all elements that have a certain value or fulfill a certain criterion from a collection. In C++, this could be achieved using a hand-written loop. It is, however, preferred to use an algorithm from the C++ Standard Library for such tasks.[1][2][3]

The algorithm library provides the remove and remove_if algorithms for this. Because these algorithms operate on a range of elem

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值