- list[meta header]
- std[meta namespace]
- list[meta class]
- function template[meta id-type]
template <class Predicate>
void remove_if(Predicate pred); // (1) C++03
template <class Predicate>
size_type remove_if(Predicate pred); // (1) C++20条件一致する全ての要素を削除する
コンテナの全ての要素に対する各イテレータiにおいて、pred(*i) == trueとなる要素を削除する。
削除された要素に対するイテレータおよび参照は無効となる。
- C++03 : なし
- C++20 : 削除された要素数を返す
pred呼び出しが例外を投げなければ、この関数は例外を投げない
ちょうどsize()回だけ述語を適用する
#include <iostream>
#include <list>
int main()
{
std::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