std::list::...ve,remove_if(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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. ✨

  1. removes all elements that are equal to value (using operator==).
  2. removes all elements for which predicate p returns true.

πŸ’– Parameters πŸ’–

  • value β€” value of the elements to remove. literally the thing you’re matching against. 🎯
  • p β€” unary predicate which returns true if 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 🌸

  • UnaryPredicate must meet the requirements of Predicate.

πŸŽ€ Return value πŸŽ€

  • (none) β€” until C++20
  • the number of elements removed β€” since C++20

πŸ”₯ Complexity πŸ”₯

given N as std::distance(begin(), end()):

  1. exactly N comparisons using operator==.
  2. exactly N applications of the predicate p.

πŸ‘‘ Notes πŸ‘‘

feature-test macro vibes:

  • __cpp_lib_list_remove_return_type β€” value 201806L, 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 to C++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 criteria
  • remove_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