std:: bind1st, std:: bind2nd
From cppreference.net
<
cpp
|
utility
|
functional
C++
Utilities library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Function objects
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Old binders and adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
ヘッダーで定義
<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
)
)
を呼び出します。
目次 |
パラメータ
| 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)
|
可変個の引数を順番に関数オブジェクトにバインドする
(関数テンプレート) |