std::binary_function - cppreference.com
来自cppreference.com
template< class Arg1, class Arg2, class Result > struct binary_function; |
(C++11 弃用) (C++17 移除) |
|
binary_function 是用于创建拥有两个实参的函数对象的基类。
binary_function 不定义 operator();它期待派生类定义此运算符。binary_function 只提供三个类型——first_argument_type、second_argument_type 和 result_type——它们由模板形参定义。
一些标准库函数对象适配器,如 std::not2,要求其适配的函数对象必须定义某些类型;std::not2 要求所适配的函数对象必须拥有两个名为 first_argument_type 和 second_argument_type 的类型。从 binary_function 派生接受两个实参的函数对象是一种令它们与这些适配器兼容的简便方式。
binary_function 于 C++11 弃用并于 C++17 移除。
成员类型
| 类型 | 定义 |
first_argument_type
|
Arg1
|
second_argument_type
|
Arg2
|
result_type
|
Result
|
示例
#include <algorithm> #include <functional> #include <iostream> #include <vector> struct same : std::binary_function<int, int, bool> { bool operator()(int a, int b) const { return a == b; } }; int main() { std::vector<char> v1{'A', 'B', 'C', 'D', 'E'}; std::vector<char> v2{'E', 'D', 'C', 'B', 'A'}; std::vector<bool> v3(v1.size()); std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), std::not2(same())); std::cout << std::boolalpha; for (std::size_t i = 0; i < v1.size(); ++i) std::cout << v1[i] << " != " << v2[i] << " : " << v3[i] << '\n'; }
输出:
A != E : true B != D : true C != C : false D != B : true E != A : true