views:

150

answers:

4

Does C++ standard library and/or Boost have anything similar to the filter function found in functional languages?

The closest function I could find was std::remove_copy_if but it seems to be doing the opposite of what I want. Does boost::lambda have any function to get a negated version of my predicate (similar to not in Haskell)? I could negate my predicate and use it with std::remove_copy_if then.

Please note that I am not asking how to write filter function in C++; I am just asking whether standard library and/or Boost already provide such a function.

Thanks in advance.

+4  A: 

There is an equivalent to filter in 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;
}
Thomas Petit
+1, This is a nice solution as well.
missingfaktor
+3  A: 

Include <functional> for std::not1 and try cont.erase (std::remove_if (cont.begin (), cont.end (), std::not1 (pred ())), cont.end ());

wilx
Exactly what I wanted. Thanks! :-)
missingfaktor
Aah, wait. I don't want the original collection to be mutated. I want a new modified copy.
missingfaktor
@Missing Faktor: then you want `remove_copy_if` instead of `remove_if`.
Mike Seymour
@Mike: Okay fine, thanks.
missingfaktor
+1  A: 

I find a lot of functional-style tasks can be solved by combining boost.iterators. For this, it has 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()));

}
Cubbi
+1  A: 

Use remove_if or remove_copy_if, with not1 (defined in <functional>) to invert the predicate. 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));
}
Mike Seymour