仿函数
Unary_function用来呈现一元函数的参数类型和返回值类型,其定义非常简单:
STL规定,每一个Adaptable Unary Function都应该继承此类别
Template <class Arg, class Result>
Struct unary_function{
Typedef Arg argument_type;
Typedef Result result_type;
};
一旦某个仿函数继承了unary_function,其用户便可以这样取得该仿函数的参数类型,并以相同手法取得其返回类型:
Template <class T>
Struct negate:public unary_function<T, T>{
T operator()(const T& x) const { return –x;}
};
Binary_function用来呈现二元函数的第一参数类型,第二参数类型,以及返回值类型,其定义非常简答:
STL规定,每一个Adaptable Binary Function都应该继承此类型
Template <c;ass Arg1, class Arg2, class Result>
Struct binary_function{
Typedef Arg1 first_argument_type;
Typedef Arg2 second_argument_type;
Typedef Result result_type;
};
一旦某个仿函数继承了binary_function,其用户便可以这样取得该仿函数的各种相应类型
Template<class T>
Struct plus : public binary_function<T, T, T>{
T operator() (const T& x, const T& y) const { return x + y; }
};
算数类仿函数
加法
Template<class T>
Struct plus : public binary_function<T, T, 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 divides : 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 operator() (const T& x) const { return -x; }
};
使用1
//生成仿函数实体
Plus<int> plusObj;
//使用上述对象
plusObj(3, 5)
使用2
直接以仿函数的临时对象履行函数功能
//function<T>()是一个临时对象,后面再接一对小括号
Plus<int>()(3, 5)
使用3
主要用途,搭配STL算法
Accumulate(iv.begin(), iv.end(), 1, multiplies<int>());
关系运算类仿函数
等于
Template<class T>
Struct equal_to : public binary_function<T, T, bool>{
T operator() (const T& x, const T& y) const { return x == y; }
};
不等于
Template<class T>
Struct not_equal_to : public binary_function<T, T, bool>{
T operator() (const T& x, const T& y) const { return x != y; }
};
大于
Template<class T>
Struct greater : public binary_function<T, T, bool>{
T operator() (const T& x, const T& y) const { return x > y; }
};
大于或等于
Template<class T>
Struct greater_equal : public binary_function<T, T, bool>{
T operator() (const T& x, const T& y) const { return x >= y; }
};
小于
Template<class T>
Struct less : public binary_function<T, T, bool>{
T operator() (const T& x, const T& y) const { return x < y; }
};
小于或等于
Template<class T>
Struct less_equal : public binary_function<T, T, bool>{
T operator() (const T& x, const T& y) const { return x <= y; }
};
使用1
Equal_to<int> equal_to_obj;
equal_to_obj(3, 5);
equal_to<int>()(3, 5);
使用2
Sort(iv.begin(), iv.end(), gerater<int>());
逻辑运算类仿函数
逻辑运算And
Template<class T>
Struct logical_and : public binary_function<T, T, bool>{
T operator() (const T& x, const T& y) const { return x && y}
};
逻辑运算Or
Template<class T>
Struct logical_or : public binary_function<T, T, bool>{
T operator() (const T& x, const T& y) const { return x || y}
};
逻辑运算Not
Template<class T>
Struct logical_not : public binary_function<T, bool>{
T operator() (const T& x) const { return !x; }
};