std::list::...ve,remove_if(3)
π std::list::...ve,remove_if(3) β C++ Standard Libary π
okay so bestie, welcome to the std::list::...ve,remove_if(3) page from the C++ Standard Libary β yes, that manual name literally says βLibaryβ in the header, weβre keeping it, no cap. this whole page is about std::list::remove,remove_if, and weβre reading it like a group chat. π
β¨ NAME β¨
std::list::remove,remove_if - std::list::remove,remove_if
periodt. thatβs the whole NAME line, and it ate. π«Ά
π¦ Synopsis π¦
okay so here are the signatures, exactly as they show up:
void remove( const T& value ); (until C++20)
size_type remove( const T& value ); (since C++20)
template< class UnaryPredicate > (1) (until C++20)
void remove_if( UnaryPredicate p ); (2)
template< class UnaryPredicate > (since C++20)
size_type remove_if( UnaryPredicate p );
these bad boys remove all elements satisfying specific criteria. the glow-up detail: they invalidate only the iterators and references to the removed elements. β¨
- removes all elements that are equal to
value(usingoperator==). - removes all elements for which predicate
preturnstrue.
π Parameters π
valueβ value of the elements to remove. literally the thing youβre matching against. π―pβ unary predicate which returnstrueif the element should be removed.
and the fine print for p, bestie: the expression p(v) must be convertible to bool for every argument v of type (possibly const) T, regardless of value category, and must not modify v. thus, a parameter type of T& is not allowed, nor is T unless for T a move is equivalent to a copy (since C++11). π
πΈ Type requirements πΈ
UnaryPredicatemust meet the requirements ofPredicate.
π Return value π
(none)β until C++20- the number of elements removed β since C++20
π₯ Complexity π₯
given N as std::distance(begin(), end()):
- exactly
Ncomparisons usingoperator==. - exactly
Napplications of the predicatep.
π Notes π
feature-test macro vibes:
__cpp_lib_list_remove_return_typeβ value201806L, std(C++20), feature: Change the return type.
π Example π
// Run this code
#include <list>
#include <iostream>
int main()
{
std::list<int> l = {1, 100, 2, 3, 10, 1, 11, -1, 12};
auto count1 = l.remove(1);
std::cout << count1 << " elements equal to 1 were removed\n";
auto count2 = l.remove_if([](int n){ return n > 10; });
std::cout << count2 << " elements greater than 10 were removed\n";
std::cout << "Finally, the list contains: ";
for (int n : l)
std::cout << n << ' ';
std::cout << '\n';
}
β¨ Output β¨
2 elements equal to 1 were removed
3 elements greater than 10 were removed
Finally, the list contains: 2 3 10 -1
π§ Defect reports π§
the following behavior-changing defect reports were applied retroactively to previously published C++ standards:
LWG 1207β applied toC++98; behavior as published: it was unclear whether iterators and/or references will be invalidated; correct behavior: only invalidates iterators and references to the removed elements.
π See also π
removeβ removes elements satisfying specific criteriaremove_if(function template)
mwah π β source: http://cppreference.com, version/date: 2024.06.10, page: std::list::...ve,remove_if(3). byeee bestie, go slay those list removals π«Άπ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms