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.
Parameters
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 . |
Return value
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
)
Complexity
Exactly std::distance(first, last) applications of the predicate.
Exceptions
The overloads with a template parameter named ExecutionPolicy
report errors as follows:
- If execution of a function invoked as part of the algorithm throws an exception and
ExecutionPolicy
is one of the three standard 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.
Notes
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 <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iterator>
using namespace std;
bool IsOdd(int x) {
return ((x%2)==1);
}
int main() {
ostream_iterator<int> output(cout," ");
vector<int>::iterator it;
vector<int> 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;
}
输出:
Elements in newnv after remove:2 4 6 8 10 6 7 8 9 10
But we want to see:2 4 6 8 10