Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::remove,std::remove_if

      From cppreference.com
      <cpp‎ |algorithm
       
       
      Algorithm library
      Constrained algorithms and algorithms on ranges(C++20)
      Constrained algorithms, e.g.ranges::copy,ranges::sort, ...
      Execution policies(C++17)
      Sorting and related operations
      Partitioning operations
      Sorting operations
      Binary search operations
      (on partitioned ranges)
      Set operations (on sorted ranges)
      Merge operations (on sorted ranges)
      Heap operations
      Minimum/maximum operations
      (C++11)
      (C++17)
      Lexicographical comparison operations
      Permutation operations
      C library
      Numeric operations
      Operations on uninitialized memory
       
      Defined in header<algorithm>
      (1)
      template<class ForwardIt,class T>
      ForwardIt remove( ForwardIt first, ForwardIt last,const T& value);
      (constexpr since C++20)
      (until C++26)
      template<class ForwardIt,class T=typenamestd::iterator_traits

                                               <ForwardIt>::value_type>
      constexpr ForwardIt remove( ForwardIt first, ForwardIt last,

                                 const T& value);
      (since C++26)
      (2)
      template<class ExecutionPolicy,class ForwardIt,class T>

      ForwardIt remove( ExecutionPolicy&& policy,

                        ForwardIt first, ForwardIt last,const T& value);
      (since C++17)
      (until C++26)
      template<class ExecutionPolicy,class ForwardIt,

               class T=typenamestd::iterator_traits
                             <ForwardIt>::value_type>
      ForwardIt remove( ExecutionPolicy&& policy,

                        ForwardIt first, ForwardIt last,const T& value);
      (since C++26)
      template<class ForwardIt,class UnaryPred>
      ForwardIt remove_if( ForwardIt first, ForwardIt last, UnaryPred p);
      (3)(constexpr since C++20)
      template<class ExecutionPolicy,class ForwardIt,class UnaryPred>

      ForwardIt remove_if( ExecutionPolicy&& policy,

                           ForwardIt first, ForwardIt last, UnaryPred p);
      (4)(since C++17)

      Removes all elements satisfying specific criteria from the range[firstlast) and returns a past-the-end iterator for the new end of the range.

      1) Removes all elements that are equal tovalue (usingoperator==).
      3) Removes all elements for which predicatep returnstrue.
      2,4) Same as(1,3), but executed according topolicy.
      These overloads participate in overload resolution only if all following conditions are satisfied:

      std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> istrue.

      (until C++20)

      std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> istrue.

      (since C++20)


      If thevalue type ofForwardIt is notCopyAssignable, the behavior is undefined.

      (until C++11)

      If the type of*first is notMoveAssignable, the behavior is undefined.

      (since C++11)

      Contents

      [edit]Explanation

      Removing is done by shifting the elements in the range in such a way that the elements that are not to be removed appear in the beginning of the range.

      • Shifting is done bycopy assignment(until C++11)move assignment(since C++11).
      • The removing operation is stable: the relative order of the elements not to be removed stays the same.
      • The underlying sequence of[firstlast) is not shortened by the removing operation. Givenresult as the returned iterator:
      • Each element of[resultlast) has a valid but unspecified state, because move assignment can eliminate elements by moving from elements that were originally in that range.
      (since C++11)

      [edit]Parameters

      first, last - the pair of iterators defining therange of elements to process
      value - the value of elements to remove
      policy - theexecution policy to use
      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)VT, whereVT is the value type ofForwardIt, regardless ofvalue category, and must not modifyv. Thus, a parameter type ofVT&is not allowed, nor isVT unless forVT a move is equivalent to a copy(since C++11).​

      Type requirements
      -
      ForwardIt must meet the requirements ofLegacyForwardIterator.
      -
      UnaryPredicate must meet the requirements ofPredicate.

      [edit]Return value

      Past-the-end iterator for the new range of values (if this is notend, then it points to an unspecified value, and so do iterators to any values between this iterator andend).

      [edit]Complexity

      Given\(\scriptsize N\)N asstd::distance(first, last):

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

      [edit]Exceptions

      The overloads with a template parameter namedExecutionPolicy report errors as follows:

      • If execution of a function invoked as part of the algorithm throws an exception andExecutionPolicy is one of thestandard policies,std::terminate is called. For any otherExecutionPolicy, the behavior is implementation-defined.
      • If the algorithm fails to allocate memory,std::bad_alloc is thrown.

      [edit]Possible implementation

      remove (1)
      template<class ForwardIt,class T=typenamestd::iterator_traits<ForwardIt>::value_type>ForwardIt remove(ForwardIt first, ForwardIt last,const T& value){    first=std::find(first, last, value);if(first!= last)for(ForwardIt i= first;++i!= last;)if(!(*i== value))*first++= std::move(*i);return first;}
      remove_if (3)
      template<class ForwardIt,class UnaryPred>ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPred p){    first=std::find_if(first, last, p);if(first!= last)for(ForwardIt i= first;++i!= last;)if(!p(*i))*first++= std::move(*i);return first;}

      [edit]Notes

      A call toremove is typically followed by a call to a container'serase member function to actually remove elements from the container. These two invocations together constitute a so-callederase-remove idiom.

      The same effect can also be achieved by the following non-member functions:

      (since C++20)

      The similarly-named containermember functionslist::remove,list::remove_if,forward_list::remove, andforward_list::remove_if erase the removed elements.

      These algorithms cannot be used with associative containers such asstd::set andstd::map because their iterator types do not dereference toMoveAssignable types (the keys in these containers are not modifiable).

      The standard library also defines an overload ofstd::remove in<cstdio>, which takes aconstchar* and is used to delete files.

      Becausestd::remove takesvalue by reference, it can have unexpected behavior if it is a reference to an element of the range[firstlast).

      Feature-test macroValueStdFeature
      __cpp_lib_algorithm_default_value_type202403(C++26)List-initialization for algorithms(1,2)

      [edit]Example

      The following code removes all spaces from a string by shifting all non-space characters to the left and then erasing the extra. This is an example oferase-remove idiom.

      Run this code
      #include <algorithm>#include <cassert>#include <cctype>#include <complex>#include <iomanip>#include <iostream>#include <string>#include <string_view>#include <vector> int main(){std::string str1{"Quick  Red  Dog"};std::cout<<"1) "<<std::quoted(str1)<<'\n';constauto noSpaceEnd= std::remove(str1.begin(), str1.end(),' ');std::cout<<"2) "<<std::quoted(str1)<<'\n'; // The spaces are removed from the string only logically.// Note, we use view, the original string is still not shrunk:std::cout<<"3) "<<std::quoted(std::string_view(str1.begin(), noSpaceEnd))<<", size: "<< str1.size()<<'\n';     str1.erase(noSpaceEnd, str1.end());// The spaces are removed from the string physically.std::cout<<"4) "<<std::quoted(str1)<<", size: "<< str1.size()<<'\n'; std::string str2="Jumped\n Over\tA\vLazy\t  Fox\r\n";    str2.erase(std::remove_if(str2.begin(),                               str2.end(),[](unsignedchar x){returnstd::isspace(x);}),               str2.end());std::cout<<"5) "<<std::quoted(str2)<<'\n'; std::vector<std::complex<double>> nums{{2,2},{1,3},{4,8}};#ifdef __cpp_lib_algorithm_default_value_type        nums.erase(std::remove(nums.begin(), nums.end(),{1,3}), nums.end());#else        nums.erase(std::remove(nums.begin(), nums.end(),std::complex<double>{1,3}),                   nums.end());#endifassert((nums==std::vector<std::complex<double>>{{2,2},{4,8}}));}

      Output:

      1) "Quick  Red  Dog"2) "QuickRedDog Dog"3) "QuickRedDog", size: 154) "QuickRedDog", size: 115) "JumpedOverALazyFox"

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 283C++98T was required to beEqualityComparable, but
      the value type ofForwardIt is not alwaysT
      required the value type ofForwardIt
      to beCopyAssignable instead

      [edit]See also

      copies a range of elements omitting those that satisfy specific criteria
      (function template)[edit]
      removes consecutive duplicate elements in a range
      (function template)[edit]
      removes elements satisfying specific criteria
      (algorithm function object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/remove&oldid=180640"

      [8]ページ先頭

      ©2009-2025 Movatter.jp