std::
binary_function
类模板 <functional>
C++98 |
template <class Arg1, class Arg2, class Result> struct binary_function;
|
C++11 |
template <class Arg1, class Arg2, class Result> struct binary_function; // deprecated |
二元函数对象基类
注意:这个类在c++ 11中已弃用。
这是标准二元函数对象的基类。
一般来说,函数对象是定义了成员函数operator()的类的实例。该成员函数允许使用与常规函数调用相同的语法来使用对象,
因此当期望使用泛型函数类型时,可以将其类型用作模板参数。
对于二元函数对象,这个operator()成员函数接受两个形参。
Binary_function只是一个基类,从它派生出特定的二元函数对象。
它没有定义operator()成员(期望派生类定义哪些成员)—它只有三个公共数据成员,它们是模板形参的类型定义。定义为:
template <class Arg1, class Arg2, class Result>
struct binary_function {
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
}; |
☲ 成员类型
成员类型 |
定义 |
注释 |
first_argument_type |
第一个模板参数(Arg1) |
成员operator()中第一个参数的类型 |
second_argument_type |
第二个模板参数(Arg2) |
成员operator()中第二个参数的类型 |
result_type |
第三个模板参数(Result) |
成员operator()返回的类型 |
☣ 示例
// binary_function example
#include <iostream> // std::cout, std::cin
#include <functional> // std::binary_function
struct Compare : public std::binary_function<int,int,bool> {
bool operator() (int a, int b) {return (a==b);}
};
int main () {
Compare Compare_object;
Compare::first_argument_type input1;
Compare::second_argument_type input2;
Compare::result_type result;
std::cout << "Please enter first number: ";
std::cin >> input1;
std::cout << "Please enter second number: ";
std::cin >> input2;
result = Compare_object (input1,input2);
std::cout << "Numbers " << input1 << " and " << input2;
if (result)
std::cout << " are equal.\n";
else
std::cout << " are not equal.\n";
return 0;
}
|
可能输出:
Please enter first number: 2
Please enter second number: 33
Numbers 2 and 33 are not equal.
🍄 另请参阅