stl源码——仿函数

仿函数

 // 仿函数分类
    // 算术类 逻辑运算类 相对关系类

    // 所有stl仿函数都需要继承binary_function<T, T, bool> 或者 unary_function<T, bool>
    // 只有继承自这两种父类,声明的仿函数才能融入stl

一元

// 一元运算符
    template <class T, class R>
    struct unary_function
    {
        using arguement_type = T;
        using result_type = R;
    };

    // 二元运算符
    template <class T1, class T2, class R>
    struct binary_function
    {
        using first_argument_type = T1;
        using second_argument_type = T2;
        using result_type = R;
    };

6个算术仿函数

// 六个算术类仿函数
    template <class T>
    struct plus : public binary_function<T, T, T>
    {
        // arguement_type = result_type = T;
        T operator()(const T& x, const T& y) const
        {
            return x + y;
        }
    };

    template <class T>
    struct minus : public binary_function<T, T, T>
    {
        T operator()(const T& x, const T& y) const
        {
            return x - y;
        }
    };

    template <class T>
    struct multiplies : public binary_function<T, T, T>
    {
        T operator()(const T& x, const T &y) const
        {
            return x * y;
        }
    };

    template <class T>
    struct divide : public binary_function<T, T, T>
    {
        T operator()(const T& x, const T& y) const 
        {
            return x / y;
        }
    };

    template <class T>
    struct modulus : public binary_function<T, T, T>
    {
        T operator()(const T& x, const T& y) const
        {
            return x % y;
        }
    };

    template <class T>
    struct negate : public binary_function<T, T, T>
    {
        T operator()(const T& x) const
        {
            return -x;       
        }
    };

    // 
    template <class T>
    inline T identify_element(plus<T>)
    {
        return T(0);
    }

    template <class T>
    inline T identify_element(multiplies<T>)
    {
        return T(1);
    }


    template <class T>
    struct equal_to : public binary_function<T, T, bool> {
        bool operator()(const T &x, const T &y) const { return x == y; }
    };

    template <class T>
    struct not_equal_to : public binary_function<T, T, bool>
    {
        bool operator()(const T& x, const T& y) const
        {
            return x != y;
        }
    };

    template <class T>
    struct greater : public binary_function<T, T, bool>
    {
        bool operator()(const T& x, const T& y) const
        {
            return x > y;
        }
    };

    template <class T>
    struct less : public binary_function<T, T, bool>
    {
        bool operator()(const T& x, const T& y) const
        {
            return x < y;
        }
    };

    template <class T>
    struct greater_equal : public binary_function<T, T, bool>
    {
        bool operator()(const T& x, const T& y) const
        {
            return x >= y;
        }
    };

    template <class T>
    struct less_equal : public binary_function<T, T,bool>
    {
        bool operator()(const T& x, const T& y) const
        {
            return x <= y;
        }
    };

    template <class T>
    struct logical_and : public binary_function<T, T, bool>
    {
        bool operator()(const T& x, const T& y) const
        {
            return x && y;
        }
    };

    template <class T>
    struct logical_or : public binary_function<T, T, bool>
    {
        bool operator()(const T& x, const T& y) const
        {
            return x || y;
        }
    };

    template <class T>
    struct logical_not : public binary_function<T, T, bool>
    {
        bool operator()(const T& x) const
        {
            return !x;
        }
    };

    template <class T>
    struct identify : public unary_function<T,T>
    {
        const T& operator()(const T& x) const
        {
            return x;
        }
    };
 // 泛型里面是一对的这种类型  比如与map  选择其中的第一个的  并返回引用 说明可以读也可以写
    // T肯定也是继承的unary 或者binary  T一般为类似map这样的数据类型map<k,v> k=first_type v = second_type
    template<class T>
    struct select1st: public unary_function<T, typename T::first_type>
    {
        const typename T::first_type& operator()(const T& x)
        {
            return x.first;
        }
    };

    template <class T>
    struct select2nd : public unary_function<T, typename T::second_type>
    {
        const typename T::second_type& operator()(const T& x)
        {
            // 返回引用一般都是可以写的  除非是const
            return x.second;
        }
    };

    // 获取输入的第一个参数? 玩呢?
    template <class T, class R>
    struct project1st : public binary_function<T, R, T>
    {
        T operator()(const T& x, const R& y) const
        {
            return x;
        }
    };

    template <class T, class R>
    struct project2nd : public binary_function<T, R, T>
    {
        R operator()(const T& x, const T& y) const
        {
            return y;
        }
    };

// 函数适配器:绑定适配器,组合,指针函数,成员函数四种
// 对函数的返回值进行进一步操作,或则加上多余的参数,不能直接带入算法
// 比如扩展函数参数接口,函数有一个参数,在扩展一下,可以传递两个接口
//适配器的本质,就是适配器类内保留一个要适配对象,比如仿函数适配器,内部就保留了一个仿函数对象,pred
// 这样就是可以通过适配器对象操作仿函数对象,并对仿函数对象的参数和结果进行修改
    // T 肯定是一个继承自unary_function的类型
    template <class T>
    struct unary_negate : public unary_function<typename T::argument_type, bool> // 这里的T就是一个仿函数类型
    {
    protected:
        T pred; // :unary_function

    public:
        explicit unary_negate(const T& x) : pred(x){}
        bool operator()(const typename T::arguemnt_type& x) const
        {
            return !pred(x); // 调用构造函数?
        }
    };

    template <class T>
    inline unary_negate<T> not1(const T & pred)
    {
        return unary_negate<T>(pred);  // 调用构造器
    }

    template <class T>
    struct binary_negate : public binary_function<typename T::first_argument_type,
                                            typename T::second_argument_type, bool>
    {
    protected:
        T pred;
    
    public:
        explicit binary_negate(const T& x) : pred(x){}
        bool operator()(const typename T::first_argument_type & x,
                        const typename T::second_argument_type & y)
        {
            return !pred(x, y);
        }
    };

    template <class T>
    inline binary_negate<T> not2(const T& pred)
    {
        return binary_negate<T>(pred);
    }
template <class T>
    struct binder1st : public unary_function<typename T::second_argument_tyep,
                                            typename T::result_type>
    {
    protected:
        T op; // T 是仿函数 继承自binaryfunc
        typename T::first_argument_type value;
    
    public:
        binder1st(const T& x, const typename T::first_arguement_type &y) 
                : op(x), value(y) {}

        typename T::result_type 
                operator()(const typename T::second_argument_type& x) const
        {
            // 这个是调用T的仿函数
            return op(value, x); // 将bind1st的值参数作为op操作的第一个参数
        }
    };

    template <class T, class R>
    inline binder1st<T> bind1st(const T& op, const R& x)
    {
        using Arg1_Type = typename T::first_argument_type;
        return binder1st<T>(op, static_cast<Arg1_Type>(x)); // 构造函数构造一个binder1st对象
    }
 template <class T>
    struct binder2nd : public unary_function<typename T::first_argument_type,
                                        typename T::result_type>
    {
    protected:
        T op;
        typename T::second_argument_type value;

    public:
        binder2nd(const T& x, const typename T::second_argument_type & y)
            :op(x), value(y) {}

        typename T::result_type 
            operator()(const typename T::first_argument_type & x) const
        {
            return op(x, value); // 将bind2nd的值参数作为op操作的第二个参数
        }
    };

    template <class T, class R>
    inline binder2nd<T> bind2nd(const T& op, const R& x)
    {
        using Arg2_type = typename T::second_argument_type;
        return binder2nd<T>(op, Arg2_type(x)); // 返回binder2nd对象 相当于会调用binder2nd仿函数  那个仿函数是一个函数操作
    }


    // 先操作R类型的仿函数,再操作T仿函数
    template <class T, class R>
    struct unary_compose : public unary_function<typename R::argument_type,
                                                typename T::result_type>
    {
    protected:
        T op1;
        R op2;
    public:
        unary_compose(const T& x, const R& y) : op1(x), op2(y) {}
        // xy都是仿函数类型,两个成员是两个仿函数,compose是将两个仿函数同时操作

        typename T::result_type operator()(const typename R::argument_type& x) const
        {
            return op1(op2(x));
        }
    };

    template <class T1, class T2>
    inline unary_compose<T1, T2> compose1(const T1& op1, const T2& op2)
    {
        return unary_compose<T1, T2>(op1, op2);  // 调用仿函数  仿函数的作用是先op2  再op1
    }

    // T2做参数,即先操作T2  T1做返回值,后操作T1
    template <class T1, class T2, class T3>
    struct binary_compose : public unary_function<typename T2::argument, typename T1::result_type>
    {
    protected:
        T1 op1;
        T2 op2;
        T3 op3;
    public:
        binary_compose(const T1& x, const T2& y, const T3& z) : op1(x), op2(y), op3(z) {}
        typename T1::result_type operator()(const typename T2::argument_type& x) const
        {
            return op1(op2(x), op3(x));
        }
    };

    template <class T1, class T2, class T3>
    inline binary_compose<T1, T2, T3> compose2(const T1& op1, const T2& op2, const T3& op3)
    {
        return binary_compose<T1, T2, T3>(op1, op2, op3);
    }

    template <class T, class R>
    struct pointer_to_unary_funtion : public unary_function<T, R>
    {
    protected:
        R (*ptr)(T); // 指向参数为T  返回值是R类型的函数指针
    public:
        pointer_to_unary_funtion(){}
        explicit pointer_to_unary_funtion(R (*x)(T)) : ptr(x){}
        R operator()(T x) const
        {
            return ptr(x); // 参数是T类型 返回值是R类型  是该函数指针的原型
        }
    };

    template <class T, class R>
    inline pointer_to_unary_funtion<T, R> ptr_fun(R (*x)(T))
    {
        return pointer_to_unary_funtion<T, R>(x);
    }

    template <class T1, class T2, class R>
    struct pointer_to_binary_function : public binary_function<T1, T2, R>
    {
    protected:
        R (*ptr)(T1, T2);

    public:
        pointer_to_binary_function(){}
        explicit pointer_to_binary_function(R (*x)(T1, T2)) : ptr(x) {}
        R operator()(T1 x, T2 y) const
        {
            return ptr(x, y);
        }
    };

    template <class T1, class T2, class R>
    inline pointer_to_binary_function<T1, T2, R> ptr_fun(R (*x)(T1, T2))
    {
        return pointer_to_binary_function<T1,T2, R>(x);
    }

    // 调用友元类中的函数f
    template <class T1, class T2>
    struct mem_fun_t : public unary_function<T1*, T2>
    {
    protected:
        explicit mem_fun_t(T1 (T2::*pf)) : f(pf) {}

        // 传入友元类的指针对象  调用友元类中的函数f
        T1 operator()(T2* p) const
        {
            return (p->*f)();
        }

    private:
        T1 (T2::*f)(); // 指向友元类T2中函数指针 无参  返回类型T1
    };

    template <class T1, class T2>
    struct const_mem_fun_t : public unary_function<const T1*, T2>
    {
    public :
        explicit const_mem_fun_t(T1 (T2::*pf)() const) : f(pf) {}
        T1 operator()(const T1 *p) const
        {
            return (p->*f)();
        }

        // T1 operator()(const T* p) const
        // {
        //     return (p->*f)();
        // }

    private:
        T1 (T2::*f)() const;
    };

    template <class T1, class T2>
    struct mem_fun_ref_t : public unary_function<T1, T2>
    {
    public:
        explicit mem_fun_ref_t(T1 (T2::*pf)()) : f(pf){}  // T2类中的函数指针pf
        T1 operator()(T2 &r) const
        {
            return (r.f)(); // 调用T2对象的()运算符
        }

        T1 (T2::*f)(); // 保存T2类中的成员函数的函数指针
    };

    template <class T1, class T2>
    struct const_mem_fun_ref_t : public unary_function<const T1, T2>
    {
    public:
        explicit const_mem_fun_ref_t(T1 (T2::*pf)() const) : f(pf){}
        T1 operator()(const T2& r) const
        {
            return (r.*f)();
        }
    private:
        T1 (T2::*f)() const;
    };

   template <class S, class T, class Arg>
    struct mem_fun1_t : public binary_function<T *, Arg, S> {
    public:
        explicit mem_fun1_t(S (T::*pf)(Arg)) : f(pf) {}
        S operator()(T *p, Arg x) const { return (p->*f)(x); }

    private:
        S (T::*f)(Arg);
    };

    template <class S, class T, class Arg>
    struct const_mem_fun1_t : public binary_function<const T *, Arg, S> {
    public:
        explicit const_mem_fun1_t(S (T::*pf)(Arg) const) : f(pf) {}
        S operator()(const T *p, Arg x) const { return (p->*f)(x); }

    private:
        S (T::*f)(Arg) const;
    };

    template <class T1, class T2, class T3>
    struct mem_fun1_ref_t : binary_function<T2, T3, T1>
    {
    public:
        explicit mem_fun1_ref_t(T1 (T2::*pf)(T3 x)) : f(pf){}
        T1 operator()(T2 & r, T3 x) const
        {
            return (r.*f)(x);
        }

        private:
        T1 (T2::*f)(T1);
    };

    template <class T1, class T2, class T3>
    struct const_mem_fun1_ref_t : binary_function<T2, T3, T1>
    {
    public:
        explicit const_mem_fun1_ref_t(T1 (T2::*pf)(T3) const) : f(pf) {}
        T1 operator()(const T2& r, T3 x) const
        {
            return (r.*f)(x);
        }
    private:
        T1 (T2::*f)(T1) const;
    };


    template <class T>
    struct mem_fun_t<void, T> : public unary_function<T*, void>
    {
    private:
        void (T::*f)();
    public:
        explicit mem_fun_t(void (T::*pf)()) : f(pf){}
        void operator()(T *p) const
        {
            (p->*f)();
        }
    };

    template <class T>
    struct const_mem_fun_t<void, T> : unary_function<const T*, void>
    {
    private:
        void (T::*f)() const;

    public:
        explicit const_mem_fun_t(void (T::*pf)()) : f(pf) {}
        void operator()(const T* p) const
        {
            (p->*f)();
        }
    };

    template <class T>
    struct mem_fun_ref_t<void, T> : public unary_function<T, void>
    {
    public:
        explicit mem_fun_ref_t(void (T::*pf)()) : f(pf){}
        void operator()(T& r) const
        {
            (r.*f)();
        }
    private:
        void (T::*f)();
    };

    template <class T>
    struct const_mem_fun_ref_t<T, void> : public unary_function<T, void>
    {
        void (T::*f)() const;

    public :
        explicit const_mem_fun_ref_t(void (T::*pf)() const) : f(pf){}
        void operator()(const T& r) const
        {
            (r.*f)();
        }
    };

    template <class T, class Arg>
    struct mem_fun1_t<void, T, Arg> : public binary_function<T *, Arg, void> {
    public:
        explicit mem_fun1_t(void (T::*pf)(Arg)) : f(pf) {}
        void operator()(T *p, Arg x) const { (p->*f)(x); }

    private:
        void (T::*f)(Arg);
    };

    template <class T, class Arg>
    struct const_mem_fun1_t<void, T, Arg>
        : public binary_function<const T *, Arg, void> {
    public:
        explicit const_mem_fun1_t(void (T::*pf)(Arg) const) : f(pf) {}
        void operator()(const T *p, Arg x) const { (p->*f)(x); }

    private:
        void (T::*f)(Arg) const;
    };

    template <class T, class R>
    struct mem_fun1_ref_t<T , R, void> 
                : public binary_function<T , R, void>
    {
    public :
        explicit mem_fun1_ref_t(void (T::*pf)(R)) : f(pf) {}
        void operator()(T& r, R x) const
        {
            (r.*f)(x);
        }
    private:
        void (T::*f)(R);
    };

    template <class T, class Arg>
    struct const_mem_fun1_ref_t<void, T, Arg>
        : public binary_function<T, Arg, void>
    {
    public:
        explicit const_mem_fun1_ref_t(void (T::*pf)(Arg) const) : f(pf) {}
        void operator()(const T &r, Arg x) const { (r.*f)(x); }

    private:
        void (T::*f)(Arg) const;
    };
// 成员方法, 调用仿函数
    template <class T1, class T2>
    inline mem_fun_t<T1, T2> mem_fun(T1 (T2::*f)())
    {
        return mem_fun_t<T1, T2>(f);
    }

    template <class T1, class T2>
    inline const_mem_fun_t<T1, T2> mem_fun(T1 (T2::*f)() const)
    {
        return const_mem_fun_t<T1, T2>(f);
    }

    template <class T1, class T2>
    inline mem_fun_ref_t<T1,T2> mem_fun_ref(T1 (T2::*f)())
    {
        return mem_fun_ref_t<T1,T2>(f);
    }

    template <class T1, class T2>
    inline const_mem_fun_ref_t<T1, T2> mem_fun_ref(T1 (T2::*f)())
    {
        return const_mem_fun_ref_t<T1, T2>(f);
    }

    template <class T1, class T2, class T3>
    inline mem_fun1_t<T1, T2, T3> mem_fun(T1 (T2::*f)(T3))
    {
        return mem_fun1_t<T1, T2, T3>(f);
    }

    template <class T1, class T2, class T3>
    inline mem_fun1_ref_t<T1, T2, T3> mem_fun_ref(T1 (T2::*f)(T3))
    {
        return mem_fun1_ref_t<T1, T2,T3>(f);
    }

    template <class T1, class T2, class T3>
    inline const_mem_fun1_ref_t<T1, T2, T3> mem_fun_ref(T1 (T2::*f)(T3) const)
    {
        return const_mem_fun1_ref_t<T1, T2, T3>(f);
    }

    template <class T1, class T2, class T3>
    inline mem_fun1_t<T1, T2, T3> mem_fun1(T1 (T2::*f)(T3))
    {
        return mem_fun1_t<T1, T2, T3>(f);
    }

    template<class T1, class T2, class T3>
    inline const_mem_fun1_t<T1, T2, T3> mem_fun1(T1 (T2::*f)(T3) const)
    {
        return const_mem_fun1_t<T1, T2, T3>(f);
    }

    template <class T1, class T2, class T3>
    inline mem_fun1_ref_t<T1, T2, T3> mem_fun1_ref(T1 (T2::*f)(T3))
    {
        return mem_fun1_ref_t<T1, T2, T3>(f);
    }

    template <class T1, class T2, class T3>
    inline const_mem_fun1_ref_t<T1, T2, T3> mem_fun_ref(T1 (T2::*f)(T3))
    {
        return const_mem_fun1_ref_t<T1, T2, T3>(f);
    }

pair类型

template <class T1, class T2>
    struct pair
    {
        using first_type = T1;
        using second_type = T2;
        
        first_type first;
        second_type second;

        pair() : first(first_type()), second(second_type()){}
        pair(const first_type& a, const second_type& b) : first(a), second(b){}

        // (10, 11) ---> (10.0, 11,0)
        template<class R1, class R2>
        pair(const pair<R1, R2>& rhs) : first(rhs.first), second(rhs.second) {}
    };
    template <class T1, class T2>
    inline bool operator==(const pair<T1, T2>& lhs, const pair<T1, T2>& rhs)
    {
        return lhs.first == rhs.first && lhs.second = rhs.second;
    }

    template <class T1, class T2>
    inline bool operator!=(const pair<T1, T2>& lhs, const pair<T1, T2>& rhs)
    {
        return !(lhs == rhs);
    }

    template<class T1, class T2>
    inline bool operator<(const pair<T1, T2>& lhs, const pair<T1,T2>& rhs)
    {
        return lhs.first < rhs.first && lhs.second < rhs.second;
    }

    template <class T1, class T2>
    inline bool operator>(const pair<T1, T2>& lhs, const pair<T1, T2>& rhs)
    {
        return lhs.first > rhs.first && lhs.second > rhs.second;
    }

    template <class T1, class T2>
    inline bool operator<=(const pair<T1, T2>& lhs, const pair<T1, T2>& rhs)
    {
        return !(lhs > rhs);
    }

    template <class T1, class T2>
    inline bool operator>=(const pair<T1, T2>& lhs, const pair<T1, T2>& rhs)
    {
        return !(lhs < rhs);
    }

    template <class T1, class T2>
    inline pair<T1, T2> make_pair(const T1& first, const T2& second)
    {
        return pair<T1, T2>(first, second);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值