一、unary_function的介绍
unary_function可以作为一个一元函数对象的基类,它只定义了参数和返回值的类型,本身并不重载()操作符,这个任务应该交由派生类去完成。
源码
template <class Arg, class Result> struct unary_function;
template <class Arg, class Result> // 1个参数,一个返回值
struct unary_function {
typedef Arg argument_type;
typedef Result result_type;
};
二、使用实例
// unary_function example
#include <iostream> // std::cout, std::cin
#include <functional> // std::unary_function
struct IsOdd : public std::unary_function<int,bool> {
bool operator() (int number) {return (number%2!=0);}
};
int main () {
IsOdd IsOdd_object;
IsOdd::argument_type input;
IsOdd::result_type result;
std::cout << "Please enter a number: ";
std::cin >> input;
result = IsOdd_object (input);
std::cout << "Number " << input << " is " << (result?"odd":"even") << ".\n";
return 0;
}
三、binary_function介绍
binary_function可以作为一个二元函数对象的基类,它只定义了参数和返回值的类型,本身并不重载()操作符,这个任务应该交由派生类去完成。
源码
template <class Arg1, class Arg2, class Result> struct binary_function;
template <class Arg1, class Arg2, class Result>
struct binary_function {
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};
四、使用实例
#include <iostream> // std::cout, std::cin
#include <functional> // std::binary_function
struct TCompareNumSize : public std::binary_function<int,int, int>{
int operator() (int num1, int num2)
{
return num1 < num2 ? num2 : num1;
}
};
int main(){
TCompareNumSize oCompareSize;
int iMaxNum = oCompareSize(1,2);
std::cout<<" 最大数是:"<<iMaxNum<<endl;
return 0;
}
参考:
https://www.cnblogs.com/blueoverflow/p/4738964.html