STL分析(八仿函数)-创新互联

//算术类
templatestruct plus : public binary_function{T operator()(const T& x, const T& y) const
        {return x + y; }
};
 
templatestruct minus : public binary_function{T operator()(const T& x, const T& y) const
        {return x - y; }
};
 
 
//逻辑运算类
templatestruct logical_and : public binary_function{T operator()(const T& x, const T& y) const
        {return x && y; }
};
 
 
//相对关系类
templatestruct equal_to : public binary_function{T operator()(const T& x, const T& y) const
        {return x == y; }
};
 
templatestruct less : public binary_function{T operator()(const T& x, const T& y) const
        {return x< y; }
};

可以看到所有标准库提供的functor都继承了binary_function,有的是unary_function,这些就是融入了STL的条件。
调用:

网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了青羊免费建站欢迎大家使用!
#include // std::cout
#include// std::plus
#include     // std::transform
using namespace std;
int main(void)
{cout<< minus()(10,5)<< endl;//5
    cout<< multiplies()(10,5)<< endl;//50
    cout<< divides()(10,5)<< endl;//2
    cout<< modulus()(10,5)<< endl;//0
    cout<< negate()(10)<< endl;//-10
    return 0;
}

在这里插入图片描述
继承后的unary_function和binary_function占0字节,不占用内存,但赋予了仿函数一对typedef,继承了这些函数的仿函数叫做“可适配的仿函数”。当仿函数需要被适配时,适配器会问仿函数一些问题,只有继承了这些typedef的仿函数可以回答这些问题。

GNU C++独有的functors

在这里插入图片描述

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网站标题:STL分析(八仿函数)-创新互联
本文链接:http://myzitong.com/article/depppd.html