Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::forward_list<T,Allocator>::remove, remove_if

      From cppreference.com
      <cpp‎ |container‎ |forward list

      [edit template]
       
       
       
      std::forward_list
      Member functions
      Non-member functions
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)
      Deduction guides(C++17)
       
      (1)
      void remove(const T& value);
      (since C++11)
      (until C++20)
      size_type remove(const T& value);
      (since C++20)
      (constexpr since C++26)
      (2)
      template<class UnaryPred>
      void remove_if( UnaryPred p);
      (since C++11)
      (until C++20)
      template<class UnaryPred>
      size_type remove_if( UnaryPred p);
      (since C++20)
      (constexpr since C++26)

      Removes all elements satisfying specific criteria.

      1) Removes all elements that are equal tovalue (usingoperator==).
      2) Removes all elements for which predicatep returnstrue.

      Invalidates only the iterators and references to the removed elements.

      Contents

      [edit]Parameters

      value - value of the elements to remove
      p - unary predicate which returns ​true if the element should be removed.

      The expressionp(v) must be convertible tobool for every argumentv of type (possibly const)T, regardless ofvalue category, and must not modifyv. Thus, a parameter type ofT&is not allowed, nor isT unless forT a move is equivalent to a copy(since C++11).​

      Type requirements
      -
      UnaryPred must meet the requirements ofPredicate.

      [edit]Return value

      (none)

      (until C++20)

      The number of elements removed.

      (since C++20)

      [edit]Complexity

      Given\(\scriptsize N\)N asstd::distance(begin(), end()):

      1) Exactly\(\scriptsize N\)N comparisons usingoperator==.
      2) Exactly\(\scriptsize N\)N applications of the predicatep.

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_list_remove_return_type201806L(C++20)Change the return type

      [edit]Example

      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:

      2 elements equal to 1 were removed3 elements greater than 10 were removedFinally, the list contains: 2 3 10 -1

      [edit]See also

      removes elements satisfying specific criteria
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/forward_list/remove&oldid=156309"

      [8]ページ先頭

      ©2009-2025 Movatter.jp