std::binary_function
来自cppreference.com
< cpp | utility | functional
在标头 <functional> 定义
|
||
template< class Arg1, |
(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
参阅
(C++11) |
任意可复制构造的可调用对象的可复制包装 (类模板) |
(C++23) |
任意可调用对象的仅移动包装,支持给定调用签名中的限定符 (类模板) |
(deprecated in C++11)(C++17 中移除) |
从函数指针创建与适配器兼容的函数对象包装器 (函数模板) |
(C++11 中弃用)(C++17 中移除) |
适配器兼容的包装器,用于包装二元函数的指针 (类模板) |
(C++11 中弃用)(C++17 中移除) |
与适配器兼容的一元函数基类 (类模板) |