std::binary_function
来自 zh.cppreference.net
| 定义于头文件 <functional>
|
||
template<
class Arg1,
class Arg2,
class Result
> struct binary_function;
|
(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)
|
适配器兼容的一元函数基类
(类模板) |