Namespaces
Variants

std:: bind1st, std:: bind2nd

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
bind1st bind2nd
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
ヘッダーで定義 <functional>
template < class F, class T >
std:: binder1st < F > bind1st ( const F & f, const T & x ) ;
(1) (C++11で非推奨)
(C++17で削除)
template < class F, class T >
std:: binder2nd < F > bind2nd ( const F & f, const T & x ) ;
(2) (C++11で非推奨)
(C++17で削除)

与えられた引数 x を、指定された二項関数オブジェクト f の第一または第二パラメータにバインドします。つまり、 x を結果のラッパー内に格納し、これが呼び出されると、 x f の第一または第二パラメータとして渡します。

1) f の第1引数を x にバインドします。実質的に std:: binder1st < F > ( f, typename F :: first_argument_type ( x ) ) を呼び出します。
2) f の第2引数を x にバインドします。実質的に std:: binder2nd < F > ( f, typename F :: second_argument_type ( x ) ) を呼び出します。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - HTMLタグ、属性、リンク先は一切変更していません - C++関連の専門用語(Parameters、Return value、Exceptions、Example、See also)は原文のまま保持しています - 数値や書式設定は完全に維持しています

パラメータ

f - 引数をバインドする関数へのポインタ
x - バインドする引数 f

戻り値

f x をラップする関数オブジェクト。

例外

実装定義の例外をスローする可能性があります。

#include <algorithm>
#include <cmath>
#include <cstddef>
#include <functional>
#include <iomanip>
#include <iostream>
#include <vector>
int main()
{
    std::vector<double> a = {0, 30, 45, 60, 90, 180};
    std::vector<double> r(a.size());
    const double pi = std::acos(-1); // C++20以降は std::numbers::pi を使用
    std::transform(a.begin(), a.end(), r.begin(),
        std::bind1st(std::multiplies<double>(), pi / 180.0));
//  同等のラムダ式: [pi](double a) { return a * pi / 180.0; });
    for (std::size_t n = 0; n < a.size(); ++n)
        std::cout << std::setw(3) << a[n] << "° = " << std::fixed << r[n]
                  << " rad\n" << std::defaultfloat;
}

出力:

  0° = 0.000000 rad
 30° = 0.523599 rad
 45° = 0.785398 rad
 60° = 1.047198 rad
 90° = 1.570796 rad
180° = 3.141593 rad

関連項目

(C++11で非推奨) (C++17で削除)
二項関数とその引数の1つを保持する関数オブジェクト
(クラステンプレート)
(C++20) (C++23)
可変個の引数を順番に関数オブジェクトにバインドする
(関数テンプレート)