如何根据std :: remove_copy_if实现copy_if?

时间:2011-04-28 21:56:51

标签: c++ stl stl-algorithm

我制定了一个大部分时间都可以使用的解决方案:

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

// Overload that takes a function pointer
template<class ForwardIterator, class OutputIterator, class ArgType>
void copy_if(ForwardIterator begin, ForwardIterator end, OutputIterator out, bool (*inPredicate)(ArgType))
{
    typedef std::pointer_to_unary_function<ArgType, bool> Adapter;
    std::remove_copy_if(begin, end, out, std::unary_negate<Adapter>(Adapter(inPredicate)));
}

// Overload that takes a function object
template<class ForwardIterator, class OutputIterator, class Functor>
void copy_if(ForwardIterator begin, ForwardIterator end, OutputIterator out, Functor inFunctor)
{
    std::remove_copy_if(begin, end, out, std::unary_negate<Functor>(inFunctor));
}

bool is_odd(int inValue)
{
    return inValue % 2 == 1;
}

bool is_odd_const_ref(const int & inValue)
{
    return inValue % 2 == 1;
}

struct is_odd_functor : public std::unary_function<int, bool>
{
    bool operator() (const int & inValue) const { return inValue % 2 == 1; }
};

int main()
{
    std::vector<int> numbers;
    numbers.push_back(0);
    numbers.push_back(1);
    numbers.push_back(2);
    numbers.push_back(3);

    std::vector<int> copy;

    // Functor: Ok
    copy_if(numbers.begin(), numbers.end(), std::back_inserter(copy), is_odd_functor());

    // Function pointer: Ok
    copy_if(numbers.begin(), numbers.end(), std::back_inserter(copy), is_odd);

    // Function pointer that takes const ref: Compiler error
    copy_if(numbers.begin(), numbers.end(), std::back_inserter(copy), is_odd_const_ref);
    return 0;
}

它不起作用的唯一情况是函数指针采用const ref参数。这会导致以下编译器错误:

/usr/include/c++/4.2.1/bits/stl_function.h: In instantiation of ‘std::unary_negate<std::pointer_to_unary_function<const int&, bool> >’:
main.cpp:11:   instantiated from ‘void copy_if(ForwardIterator, ForwardIterator, OutputIterator, bool (*)(ArgType)) [with ForwardIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, OutputIterator = std::back_insert_iterator<std::vector<int, std::allocator<int> > >, ArgType = const int&]’
main.cpp:53:   instantiated from here
/usr/include/c++/4.2.1/bits/stl_function.h:322: error: forming reference to reference type ‘const int&’
/usr/include/c++/4.2.1/bits/stl_algo.h: In function ‘_OutputIterator std::remove_copy_if(_InputIterator, _InputIterator, _OutputIterator, _Predicate) [with _InputIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _OutputIterator = std::back_insert_iterator<std::vector<int, std::allocator<int> > >, _Predicate = std::unary_negate<std::pointer_to_unary_function<const int&, bool> >]’:
main.cpp:11:   instantiated from ‘void copy_if(ForwardIterator, ForwardIterator, OutputIterator, bool (*)(ArgType)) [with ForwardIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, OutputIterator = std::back_insert_iterator<std::vector<int, std::allocator<int> > >, ArgType = const int&]’
main.cpp:53:   instantiated from here
/usr/include/c++/4.2.1/bits/stl_algo.h:1227: error: no match for call to ‘(std::unary_negate<std::pointer_to_unary_function<const int&, bool> >) (int&)’

显然在这里尝试引用参考文献。

我的问题是:如何实施符合以下条件的copy_if

  1. 以std :: remove_copy_if
  2. 表示的实现
  3. 必须使用函数对象和函数指针
  4. 没有定义辅助类(在函数范围之外)
  5. 使用纯C ++和STL(无C ++ 0x或提升)
  6. 更新

    我认为这是可行的,因为它适用于boost::bind

    template<class ForwardIterator, class OutputIterator, class Functor>
    void copy_if(ForwardIterator begin, ForwardIterator end, OutputIterator out, Functor inFunctor)
    {
        std::remove_copy_if(begin, end, out, !boost::bind(inFunctor, _1));
    }
    

1 个答案:

答案 0 :(得分:4)

对于C ++ 03 <functional>而言,你正在做一些太多的工作。消除Adapter支持工厂功能。

http://ideone.com/2VEfH

// Overload that takes a function pointer
template<class ForwardIterator, class OutputIterator, class ArgType>
OutputIterator copy_if(ForwardIterator begin, ForwardIterator end, OutputIterator out, bool (*inPredicate)(ArgType))
{
    return std::remove_copy_if(begin, end, out, std::not1(std::ptr_fun(inPredicate)));
}

// Overload that takes a function object
template<class ForwardIterator, class OutputIterator, class Functor>
OutputIterator copy_if(ForwardIterator begin, ForwardIterator end, OutputIterator out, Functor inPredicate)
{
    return std::remove_copy_if(begin, end, out, std::not1(inPredicate));
}

另外,我会指出应将TR1添加到您决定不使用的现有常用设施列表中。

此外,如果函数指针copy_if重载称为仿函数,而不是两者都是完整的实现,那将会更优雅。

另外,正如@wilhelm指出的那样,返回类型应为OutputIterator。我已将其修复为此片段。