STL内建了一些函数对象。分为:算数类函数对象,关系运算类函数对象,逻辑运算类仿函数。这些仿函数所产生的对象,用法和一般函数完全相同,当然我们还可以产生无名的临时对象来履行函数功能。使用内建函数对象,需要引入头文件#include
。
6个算数类函数对象,除了negate是一元运算,其他都是二元运算
template T plus//加法仿函数
template T minus//减法仿函数
template T multiplies//乘法仿函数
template T divides//除法仿函数
template T modulus//取模仿函数
template T negate//取反仿函数
6个关系运算类函数对象,每一种都是二元运算
template bool equal_to//等于
template bool not_equal_to//不等于
template bool greater//大于
template bool greater_equal//大于等于
template bool less//小于
template bool less_equal//小于等于
逻辑运算类运算函数,not为一元运算,其余为二元运算
template bool logical_and//逻辑与
template bool logical_or//逻辑或
template bool logical_not//逻辑非
内建函数对象举例:
//取反仿函数
void test01()
{
negate n;
cout
关注
打赏