Logo

Programming-Idioms

History of Idiom 247 > diff from v8 to v9

Edit summary for version 9 by WHS:
New C++ implementation by user [WHS]

Version 8

2020-12-09, 17:07:34

Version 9

2020-12-27, 19:00:33

Idiom #247 Filter list in-place

Remove all the elements from list x that don't satisfy the predicate p, without allocating a new list.
Keep all the elements that do satisfy p.

For languages that don't have mutable lists, refer to idiom #57 instead.

Idiom #247 Filter list in-place

Remove all the elements from list x that don't satisfy the predicate p, without allocating a new list.
Keep all the elements that do satisfy p.

For languages that don't have mutable lists, refer to idiom #57 instead.

Variables
x,p
Variables
x,p
Extra Keywords
keep conserve preserve mutable
Extra Keywords
keep conserve preserve mutable
Imports
#include <list>
#include <functional>
Code
std::list<Foo> x;
x.remove_if(std::not_fn(p));
Comments bubble
C++17 or later
Also works if x is an std::forward_list
Doc URL
https://en.cppreference.com/w/cpp/container/list/remove