首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::binary_function

Defined in header <functional>

?

?

template< class Arg1, class Arg2, class Result > struct binary_function;

?

(until C++17)(deprecated since c++11)

binary_function是一个基类,用于创建带有两个参数的函数对象。

binary_function没有定义operator();预计派生类将定义这一点。binary_function只提供三种类型-first_argument_type,,,second_argument_typeresult_type-由模板参数定义。

一些标准库函数对象适配器,如std::not2,要求它们所适应的函数对象具有特定类型的定义;std::not2要求将函数对象调整为具有两个名为first_argument_typesecond_argument_type派生函数对象,这些对象接受两个参数。binary_function是使它们与这些适配器兼容的简单方法。

binary_function在C++11中被弃用,在C++17中删除。

成员类型

Type

Definition

first_argument_type

Arg1

second_argument_type

Arg2

result_type

Result

二次

代码语言:javascript
复制
#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<int> v1{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int> v2{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
    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';
}

二次

产出:

二次

代码语言:javascript
复制
0 10 true
1 9 true
2 8 true
3 7 true
4 6 true
5 5 false
6 4 true
7 3 true
8 2 true
9 1 true
10 0 true

二次

另见

function (C++11)

wraps callable object of any type with specified function call signature (class template)

ptr_fun (until C++17)

creates an adaptor-compatible function object wrapper from a pointer to function (function template)

pointer_to_binary_function (until C++17)

adaptor-compatible wrapper for a pointer to binary function (class template)

unary_function (until C++17)

adaptor-compatible unary function base class (class template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com