Logo

Programming-Idioms

History of Idiom 247 > diff from v10 to v11

Edit summary for version 11 by WHS:
[C++] Add to comments

Version 10

2020-12-27, 19:04:56

Version 11

2020-12-27, 19:06:40

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 <functional>
Imports
#include <functional>
Code
std::erase_if(x, std::not_fn(p));
Code
std::erase_if(x, std::not_fn(p));
Comments bubble
C++20

Compatible with most STL containers (Exceptions: x cannot be an std::array, std::stack, or std::queue)
Comments bubble
C++20

Compatible with most STL containers (Exceptions: x cannot be an std::array, std::stack, or std::queue)

std::erase_if also returns the number of elements removed