The Fourth Dimension Space

枯叶北风寒,忽然年以残,念往昔,语默心酸。二十光阴无一物,韶光贱,寐难安; 不畏形影单,道途阻且慢,哪曲折,如渡飞湍。斩浪劈波酬壮志,同把酒,共言欢! -如梦令

C++ 泛型编程练习 Adaptor Functor & Helper Function

#include<iostream>
using namespace std;

template <class OutputIterator, class Iterator, class predicate>
    void copy(OutputIterator o, Iterator f, Iterator l, predicate p)
    {
        while(f!=l)
        {
            if(p(*f))
            {
                *o = *f;
                ++f;
                ++o;
            }
            else
            {
                ++f;
            }
        }

    }
template <class Predicate>
class negate
{
    Predicate p;
public :
    typedef typename Predicate::value_type value_type;
    negate(const Predicate &pred):p(pred){}
    bool operator()(const value_type v)const
    {
        return !p(v);
    }
};

template<class Number>
struct smallerThanX
{
    typedef Number value_type;
    int x;
    smallerThanX(int x):x(x)
    {

    }

    bool operator()(const Number& n) const
    {
        return n< this->x;
    }
};

template <class Predicate>
inline negate<Predicate > NOT (const Predicate &p)
{
    return negate<Predicate>(p);
}


int main()
{
    int a[] = {1,2,3,4,5,6,7,8};
    int *b = new int [10]; 
    copy(b,a,a+8,NOT(smallerThanX<int>(5)));
    return 0;
}

这种方式实在是有点难看啊。。。 代码不仔细看还看不懂,不知道为什么要这样写呢。。。

posted on 2014-09-29 23:50 abilitytao 阅读(457) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理