remove_if的使用

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.

1) Removes all elements that are equal tovalue.
3) Removes all elements for which predicatep returns true.
2,4) Same as (1,3), but executed according topolicy. 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.
The type Type must be such that an object of typeForwardIt can be dereferenced and then implicitly converted toType.​

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 andExecutionPolicy 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 before remove:1 2 3 4 5 6 7 8 9 10
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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值