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

本文详细介绍了C++ STL中仿函数的概念和应用,包括unary_function和binary_function基类,以及一系列如加减乘除、关系比较、逻辑运算等仿函数的实现。仿函数作为算法参数,提供了自定义操作的能力,增强了算法的灵活性。
摘要由CSDN通过智能技术生成




//---------------------------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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值