Removes all elements satisfying specific criteria from the range [first, last)
and returns a past-the-end iterator for the new end of the range.
value
.
p
returns true.
policy
. These overloads do not participate in overload resolution unlessstd::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true
Removing is done by shifting (by means of move assignment) 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. Relative order of the elements that remain is preserved and thephysical size of the container is unchanged. Iterators pointing to an element between the newlogical end and the physical end of the range are still dereferenceable, but the elements themselves have unspecified values (as perMoveAssignable
post-condition). A call to remove
is typically followed by a call to a container'serase
method, which erases the unspecified values and reduces the physical size of the container to match its new logical size.
first, last | - | the range of elements to process |
value | - | the value of elements to remove |
policy | - | the execution policy to use. See execution policy for details. |
p | - | unary predicate which returns true if the element should be removed. The signature of the predicate function should be equivalent to the following: bool pred(const Type&a); The signature does not need to have const&, but the function must not modify the objects passed to it. |
Type requirements | ||
-ForwardIt must meet the requirements of ForwardIterator . |
||
-The type of dereferenced ForwardIt must meet the requirements ofMoveAssignable . |
||
-UnaryPredicate must meet the requirements of Predicate . |
Past-the-end iterator for the new range of values (if this is not end
, then it points to an unspecified value, and so do iterators to any values between this iterator andend
)
Exactly std::distance(first, last) applications of the predicate.
The overloads with a template parameter named ExecutionPolicy
report errors as follows:
ExecutionPolicy
is one of the three standard policies, std::terminate is called. For any otherExecutionPolicy
, the behavior is implementation-defined.The similarly-named container member functions list::remove, list::remove_if, forward_list::remove, and forward_list::remove_if erase the removed elements.
These algorithms cannot be used with associative containers such as std::set andstd::map because ForwardIt does not dereference to a MoveAssignable type (the keys in these containers are not modifiable)
The standard library also defines an overload of std::remove
takingconst char*
, used to delete files: std::remove.
实例解析:
#include
#include
#include
#include
#include
using namespace std;
bool IsOdd(int x) {
return ((x%2)==1);
}
int main() {
ostream_iterator output(cout," ");
vector::iterator it;
vector newnv(10);
for(int i=0;i<10;i++)
newnv[i] = i+1;
cout<<"\nElements in newnv before remove:";
copy(newnv.begin(),newnv.end(),output);
it = remove_if(newnv.begin(),newnv.end(),IsOdd);
cout<<"\nElements in newnv after remove:";
copy(newnv.begin(),newnv.end(),output);
cout<<"\nBut we want to see:";
copy(newnv.begin(),it,output);
return 0;
}
输出: