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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章