std::forwar...ve,remove_if(3)
π std::forward_list::remove, remove_if β bestie edition π
okay so welcome to the std::forwar...ve,remove_if(3) page from the C++ Standard Libary manual β yes, that's the manual name exactly as the header spells it. This is the std::forward_list::remove and remove_if situation, and we're gonna make it make sense together. π«Ά
πΈ NAME πΈ
std::forward_list::remove,remove_if β that's the name, bestie. The description line is the exact same string: std::forward_list::remove,remove_if. It's giving "the title is the title" energy, and honestly? iconic. β¨
β¨ Synopsis β¨
The synopsis on the page is a little feral, so here it is with all the exact declarations and annotations:
void remove( const T& value ); (since C++11)
(until C++20)
size_type remove( const T& value ); (since C++20)
template< class UnaryPredicate > (1) (since C++11)
void remove_if( UnaryPredicate p ); (2) (until C++20)
template< class UnaryPredicate > (since C++20)
size_type remove_if( UnaryPredicate p );
It removes all elements that satisfy the criteria. The only iterators and references that get invalidated are the ones pointing at the removed elements β not the whole container, so breathe. π
- Removes every element equal to
value, usingoperator==. - Removes every element for which the predicate
preturnstrue.
π Parameters π
valueβ the value of the elements to remove.pβ a unary predicate that returnstrueif the element should be removed.
Okay, predicate lore time: the expression p(v) must be convertible to bool for every argument v of type (possibly const) T, regardless of value category, and it must not modify v. So a parameter type of T& is not allowed, and neither 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 β
The feature-test macro table is serving:
- Feature-test macro:
__cpp_lib_list_remove_return_type - Value:
201806L - Std: (C++20)
- Feature: Change the return type
π Example π
The example starts with // Run this code and then does this:
// Run this code
#include <forward_list>
#include <iostream>
int main()
{
std::forward_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 π
And the output block is exactly:
2 elements equal to 1 were removed
3 elements greater than 10 were removed
Finally, the list contains: 2 3 10 -1
π See also π
removeβ removes elements satisfying specific criteriaremove_if(function template)
xoxo, source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::forwar...ve,remove_if(3) β catch u later, bestie ππ¦
π¦ 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