std:: binary_function
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Old binders and adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<functional>
|
||
|
template
<
class
Arg1,
|
(C++11 起弃用)
(C++17 中移除) |
|
std::binary_function
是用于创建带有两个参数的函数对象的基类。
std::binary_function
并未定义
operator
(
)
;预期由派生类定义该运算符。
std::binary_function
仅提供由模板参数定义的三种类型 -
first_argument_type
、
second_argument_type
和
result_type
。
某些标准库函数对象适配器,例如
std::not2
,要求被适配的函数对象具有特定类型定义;
std::not2
要求被适配的函数对象必须定义名为
first_argument_type
和
second_argument_type
的两种类型。从
std::binary_function
派生接受两个参数的函数对象,是使它们与这些适配器兼容的简便方法。
std::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)
(removed in C++17)
|
从函数指针创建适配器兼容的函数对象包装器
(函数模板) |
|
(deprecated in C++11)
(removed in C++17)
|
用于二元函数指针的适配器兼容包装器
(类模板) |
|
(deprecated in C++11)
(removed in C++17)
|
适配器兼容的一元函数基类
(类模板) |