简体   繁体   English

在C ++中“过滤”高阶函数

[英]“filter” higher order function in C++

Does C++ standard library and/or Boost have anything similar to the filter function found in functional languages? C ++标准库和/或Boost是否具有与函数式语言中的filter函数类似的东西?

The closest function I could find was std::remove_copy_if but it seems to be doing the opposite of what I want. 我能找到的最接近的函数是std::remove_copy_if但它似乎与我想要的相反。 Does boost::lambda have any function to get a negated version of my predicate (similar to not in Haskell)? boost::lambda是否有任何函数来获取我的谓词的否定版本(类似于Haskell中not )? I could negate my predicate and use it with std::remove_copy_if then. 我可以否定我的谓词并将其与std::remove_copy_if一起使用。

Please note that I am not asking how to write filter function in C++; 请注意,我不是在询问如何在C ++中编写filter函数; I am just asking whether standard library and/or Boost already provide such a function. 我只想问标准库和/或Boost是否已经提供了这样的功能。

Thanks in advance. 提前致谢。

There is an equivalent to filter in Boost.Range . Boost.Range中有一个等效的过滤器。
Here is an example : 这是一个例子:

#include <vector>
#include <boost/lambda/lambda.hpp>
#include <boost/range/algorithm_ext/push_back.hpp>
#include <boost/range/adaptor/filtered.hpp>

using namespace boost::adaptors;
using namespace boost::lambda;

int main()
{
    std::vector<int> v = {3, 2, 6, 10, 5, 2, 45, 3, 7, 66};
    std::vector<int> v2;
    int dist = 5;

    boost::push_back(v2, filter(v, _1 > dist));
    return 0;
}

包括std::not1 <functional>并尝试cont.erase (std::remove_if (cont.begin (), cont.end (), std::not1 (pred ())), cont.end ());

I find a lot of functional-style tasks can be solved by combining boost.iterators. 我发现通过组合boost.iterators可以解决许多功能风格的任务。 For this, it has filter_iterator . 为此,它有filter_iterator

Say, you have a vector of natural numbers, and a function that you want to apply to a pair of iterators, which should only see the filtered vector, with just the odd numbers: 比如说,你有一个自然数的向量,以及一个你想要应用于一对迭代器的函数,它只能看到过滤的向量,只有奇数:

#include <algorithm>
#include <vector>
#include <iterator>
#include <numeric>
#include <iostream>
#include <boost/iterator/filter_iterator.hpp>
template<typename Iter>
void do_stuff(Iter beg, Iter end)
{
    typedef typename std::iterator_traits<Iter>::value_type value_t;
    copy(beg, end, std::ostream_iterator<value_t>(std::cout, " "));
    std::cout << '\n';
}
struct is_even {
        bool operator()(unsigned int i) const { return i%2 == 0; }
};
int main()
{
        std::vector<unsigned int> v(10, 1);
        std::partial_sum(v.begin(), v.end(), v.begin()); // poor man's std::iota()

        // this will print all 10 numbers
        do_stuff(v.begin(), v.end());
        // this will print just the evens
        do_stuff(boost::make_filter_iterator<is_even>(v.begin(), v.end()),
                 boost::make_filter_iterator<is_even>(v.end(), v.end()));

}

Use remove_if or remove_copy_if , with not1 (defined in <functional> ) to invert the predicate. 使用remove_ifremove_copy_if ,使用not1 (在<functional>定义)来反转谓词。 Something like this: 像这样的东西:

#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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM