C++ Reference: Standard C++ Library reference: Containers: list: list: remove_if

C++官网参考链接:https://cplusplus.com/reference/list/list/remove_if/

公有成员函数模板
<list>
std::list::remove_if
template <class Predicate>  void remove_if (Predicate pred);
删除满足条件的元素
从容器中删除谓词pred返回true的所有元素。这将调用这些对象的析构函数,并根据删除的元素数量减少容器的size
该函数为每个元素调用pred(*i)(其中i是该元素的iterator)。list中任何返回true的元素都将从容器中删除。

形参 
pred
一元谓词,接受与list对象中包含的值相同类型的值,对于要从容器中删除的值返回true,对于剩余的值返回false。
它可以是函数指针,也可以是函数对象。

返回值
没有返回值。

用例
// list::remove_if
#include <iostream>
#include <list>

// a predicate implemented as a function:
bool single_digit (const int& value) { return (value<10); }

// a predicate implemented as a class:
struct is_odd {
  bool operator() (const int& value) { return (value%2)==1; }
};

int main ()
{
  int myints[]= {15,36,7,17,20,39,4,1};
  std::list<int> mylist (myints,myints+8);   // 15 36 7 17 20 39 4 1

  mylist.remove_if (single_digit);           // 15 36 17 20 39

  mylist.remove_if (is_odd());               // 36 20

  std::cout << "mylist contains:";
  for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}
输出: 
mylist contains: 36 20

复杂度
list size(pred的应用)中的线性。

iterator的有效性
指向被函数删除的元素的iterator、指针和reference将失效。
所有其他iterator、指针和reference保持它们的有效性。

数据竞争
完成容器的修改。
删除的元素被修改。同时访问或修改其他元素是安全的,但遍历容器就不安全了。

异常安全
如果pred保证不抛出,则该函数永远不会抛出异常(无抛出保证)。
否则,如果抛出异常,容器将保持有效状态(基本保证)。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_40186813

你的能量无可限量。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值