• stl源码剖析 详细学习笔记 仿函数





    //---------------------------15/04/01----------------------------



    //仿函数是为了算法而诞生的,可以作为算法的一个参数,来自定义各种操作,比如比大小,返回bool值,对元素进行操作等

    //虽然这些函数也能实现,但是如果配合配接器(adapter)可以产生更灵活的变化。

    //为了使对象像函数一样,就必须重载operator()



    //unary_function


    template<class Arg, class Result>

    struct unary_function

    {

        //参数类型

        typedef Arg argument_type;

        //返回值类型

        typedef Result result_type;

    };



    //binary_functione


    //二元仿函数

    template<class Arg1, class Arg2, class Result>

    struct binary_functione

    {

        typedef Arg1 first_argument_type;

        typedef arg2 second_argument_type;

        typedef Result result_type;

    };



    //算术类仿函数


    template<class T>

    struct plus : public binary_functione<T, T, T>

    {

        T operator()(const T& x, const T& y) const

        {

            return x + y;

        }

    };


    template<class T>

    struct minus : public binary_functione<T, T, T>

    {

        T operator()(const T& x, const T& y) const

        {

            return x - y;

        }

    };


    template<class T>

    struct multiplies : public binary_functione<T, T, T>

    {

        T operator()(const T& x, const T& y) const

        {

            return x * y;

        }

    };


    template<class T>

    struct divides : public binary_functione<T, T, T>

    {

        T operator()(const T& x, const T& y) const

        {

            return x / y;

        }

    };


    template<class T>

    struct modulus : public binary_functione<T, T, T>

    {

        T operator()(const T& x, const T& y) const

        {

            return x & y;

        }

    };


    template<class T>

    struct negate: public unary_function<T, T>

    {

        T operator()(const T& x) const

        {

            return -x;

        }

    };



    //证同元素,数值a与该元素做op操作会得到自己。 加法的证同元素为0 乘法为1

    template<class T>

    inline T identity_element(plus<T>)

    {

        return T(0);

    };


    template<class T>

    inline T identity_element(multiplies<T>)

    {

        return T(1);

    };


    //关系运算类仿函数


    template<class T>

    struct equal_to : public binary_functione<T, T, bool>

    {

        bool operator()(const T& x, const T& y) const

        {

            return x == y;

        }

    };


    template<class T>

    struct not_equal_to : public binary_functione<T, T, bool>

    {

        bool operator()(const T& x, const T& y) const

        {

            return x != y;

        }

    };


    template<class T>

    struct greater : public binary_functione<T, T, bool>

    {

        bool operator()(const T& x, const T& y) const

        {

            return x > y;

        }

    };


    template<class T>

    struct less : public binary_functione<T, T, bool>

    {

        bool operator()(const T& x, const T& y) const

        {

            return x < y;

        }

    };


    template<class T>

    struct greater_equal : public binary_functione<T, T, bool>

    {

        bool operator()(const T& x, const T& y) const

        {

            return x >= y;

        }

    };


    template<class T>

    struct less_equal : public binary_functione<T, T, bool>

    {

        bool operator()(const T& x, const T& y) const

        {

            return x <= y;

        }

    };



    //逻辑类仿函数

    template<class T>

    struct logical_and : public binary_functione<T, T, bool>

    {

        bool operator()(const T& x, const T& y) const

        {

            return x && y;

        }

    };



    template<class T>

    struct logical_or : public binary_functione<T, T, bool>

    {

        bool operator()(const T& x, const T& y) const

        {

            return x || y;

        }

    };


    template<class T>

    struct logical_not : public unary_function<T, bool>

    {

        bool operator()(const T& x) const

        {

            return !x;

        }

    };


    //证同函数,任何数通过此函数调用运算后返回原值。

    template<class T>

    struct identity : public unary_function<T, T>

    {

        const T& operator()(const T& x) const

        {

            return x;

        }

    };


    //选择函数 接受pair,传回第一个元素

    template<class Pair>

    struct select1st : public unary_function<Pair, typename Pair::first_type>

    {

        const typename Pair::first_type& operator()(const Pair& x) const

        {

            return x.first;

        }

    };


    template<class Pair>

    struct select2nd : public unary_function<Pair, typename Pair::second_type>

    {

        const typename Pair::second_type& operator()(const Pair& x) const

        {

            return x.second;

        }

    };


    //投射函数:传回第一参数,忽略第二参数

    template<class Arg1, class Arg2>

    struct project1st : public binary_functione<Arg1, Arg2, Arg1>

    {

        Arg1 operator()(const Arg1& x, const Arg2& y) const

        {

            return x;

        }

    }


    template<class Arg1, class Arg2>

    struct project2nd : public binary_functione<Arg1, Arg2, Arg2>

    {

        Arg2 operator()(const Arg1& x, const Arg2& y) const

        {

            return y;

        }

    }









  • 相关阅读:
    C++之STL总结精华笔记
    [转]asp.net使用uploadify上传出现的IO Error问题
    $.post()参数及返回值
    用CSS3.0画圆
    C# 用代码返回上一页
    用js获取cookie
    html页面的局部刷新
    在Asp.Net中使用amChart统计图
    内存中DataTable去除重复行
    .net中对HTTP请求的两种请求:Get和Post的操作
  • 原文地址:https://www.cnblogs.com/boydfd/p/4983149.html
Copyright © 2020-2023  润新知