◐ Shell
clean mode source ↗

std::binary_function - cppreference.com

<tbody> </tbody>

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

(C++11で非推奨)
(C++17で削除)

binary_function は引数を2つ取る関数を作成するための基底クラスです。

binary_functionoperator() を定義しません。 派生クラスがそれを定義することが期待されます。 binary_function はテンプレート引数によって定義される3つの型 first_argument_typesecond_argument_type および result_type のみを提供します。

いくつかの標準ライブラリの関数オブジェクトアダプタ (std::not2 など) は、アダプトする関数オブジェクトが特定の型を定義していることを要求します。 std::not2 はアダプトする関数オブジェクトが first_argument_type および second_argument_type という2つの型を持つことを要求します。 引数を2つ取る関数オブジェクトを 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<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';
}

出力:

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

関連項目