Skip to content

Latest commit

 

History

History
75 lines (53 loc) · 1.64 KB

remove_if.md

File metadata and controls

75 lines (53 loc) · 1.64 KB

remove_if

  • forward_list[meta header]
  • std[meta namespace]
  • forward_list[meta class]
  • function template[meta id-type]
  • cpp11[meta cpp]
template <class Predicate>
void remove_if(Predicate pred);      // (1) C++11

template <class Predicate>
size_type remove_if(Predicate pred); // (1) C++20

概要

条件一致する全ての要素を削除する

効果

コンテナの全ての要素に対する各イテレータiにおいて、pred(*i) == trueとなる要素を削除する。

削除された要素に対するイテレータおよび参照は無効となる。

戻り値

  • C++11 : なし
  • C++20 : 削除された要素数を返す

例外

pred呼び出しが例外を投げなければ、この関数は例外を投げない

計算量

ちょうどdistance(begin(), end())回だけ述語を適用する

#include <iostream>
#include <forward_list>

int main()
{
  std::forward_list<int> ls = {3, 1, 4, 1};

  ls.remove_if([](int x) { return x == 1; }); // 値1の要素を全て削除

  for (int x : ls) {
    std::cout << x << std::endl;
  }
}
  • remove_if[color ff0000]

出力

3
4

バージョン

言語

  • C++11

処理系

  • Clang: ??
  • GCC: 4.7.0 [mark verified]
  • ICC: ??
  • Visual C++: 2010 [mark verified], 2012 [mark verified], 2013 [mark verified], 2015 [mark verified], 2017 [mark verified]

参照