名前空間
変種
操作

std::bind1st, std::bind2nd

From cppreference.com
< cpp‎ | utility‎ | functional
 
 
ユーティリティライブラリ
言語サポート
型のサポート (基本型、RTTI)
ライブラリ機能検査マクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
 
関数オブジェクト
関数の呼び出し
(C++17)(C++23)
恒等関数オブジェクト
(C++20)
透過的な演算子ラッパー
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

古いバインダとアダプタ
(C++17まで*)
(C++17まで*)
(C++17まで*)
(C++17まで*)  
(C++17まで*)
(C++17*まで)(C++17*まで)(C++17*まで)(C++17*まで)
(C++20まで*)
(C++20まで*)
(C++17*まで)(C++17*まで)
bind1stbind2nd
(C++17*まで)(C++17*まで)

(C++17まで*)
(C++17*まで)(C++17*まで)(C++17*まで)(C++17*まで)
(C++20まで*)
(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で削除)

指定された二項関数オブジェクト f の第一引数または第二引数に、指定された引数 x をバインドします。すなわち、結果のラッパー内に x を格納し、そのラッパーが呼び出された際に、f の第一引数または第二引数として x を渡します。

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

目次

[編集] パラメータ

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

[編集] 戻り値

fx をラップした関数オブジェクト。

[編集] 例外

実装定義の例外をスローする場合があります。

[編集]

#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); // since C++20 use std::numbers::pi
 
    std::transform(a.begin(), a.end(), r.begin(),
        std::bind1st(std::multiplies<double>(), pi / 180.0));
//  an equivalent lambda is: [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で削除)
二項関数とその引数の一つを保持する関数オブジェクト
(クラステンプレート) [編集]
(C++20)(C++23)
可変個の引数を順番に関数オブジェクトに束縛する
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)