使用remove_if
or remove_copy_if
, with not1
(defined in <functional>
) 反转谓词。像这样的东西:
#include <algorithm>
#include <functional>
template <class ForwardIterator, class Predicate>
ForwardIterator filter(ForwardIterator first, ForwardIterator last,
Predicate pred)
{
return std::remove_if(first, last, std::not1(pred));
}
template <class InputIterator, class OutputIterator, class Predicate>
OutputIterator filter_copy(InputIterator first, InputIterator last,
OutputIterator result, Predicate pred)
{
return std::remove_copy_if(first, last, result, std::not1(pred));
}