std::binder1st, std::binder2nd
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <functional> で定義
|
||
template< class Fn > class binder1st : public std::unary_function<typename Fn::second_argument_type, typename Fn::result_type> { protected: Fn op; typename Fn::first_argument_type value; public: binder1st(const Fn& fn, const typename Fn::first_argument_type& value); typename Fn::result_type operator()(const typename Fn::second_argument_type& x) const; typename Fn::result_type operator()(typename Fn::second_argument_type& x) const; }; |
(1) | (C++11で非推奨) (C++17で削除) |
template< class Fn > class binder2nd : public unary_function<typename Fn::first_argument_type, typename Fn::result_type> { protected: Fn op; typename Fn::second_argument_type value; public: binder2nd(const Fn& fn, const typename Fn::second_argument_type& value); typename Fn::result_type operator()(const typename Fn::first_argument_type& x) const; typename Fn::result_type operator()(typename Fn::first_argument_type& x) const; }; |
(2) | (C++11で非推奨) (C++17で削除) |
二項関数に引数を束縛する関数オブジェクト。
引数の値は構築時にオブジェクトに渡され、オブジェクト内に格納されます。 operator() を通して関数オブジェクトが呼ばれると、格納された値が引数のひとつとして渡され、他の引数は operator() の引数として渡されます。 結果の関数オブジェクトは単項関数です。
1) 第1引数をオブジェクトの構築時に与えられた値
value に束縛します。2) 第2引数をオブジェクトの構築時に与えられた値
value に束縛します。例
Run this code
#include <iostream>
#include <functional>
#include <cmath>
#include <vector>
const double pi = std::acos(-1);
int main()
{
// C++11 で非推奨になり C++17 で削除された古い手法。
std::binder1st<std::multiplies<double>> f1 = std::bind1st(
std::multiplies<double>(), pi / 180.);
// C++11 では以下のようにします。
auto f2 = [](double a){ return a*pi/180.; };
for(double n : {0, 30, 45, 60, 90, 180})
std::cout << n << " deg = " << f1(n) << " rad (using binder) "
<< f2(n) << " rad (using lambda)\n";
}
出力:
0 deg = 0 rad (using binder) 0 rad (using lambda)
30 deg = 0.523599 rad (using binder) 0.523599 rad (using lambda)
45 deg = 0.785398 rad (using binder) 0.785398 rad (using lambda)
60 deg = 1.0472 rad (using binder) 1.0472 rad (using lambda)
90 deg = 1.5708 rad (using binder) 1.5708 rad (using lambda)
180 deg = 3.14159 rad (using binder) 3.14159 rad (using lambda)
関連項目
(C++11で非推奨)(C++17で削除) |
二項関数に引数を1つ束縛します (関数テンプレート) |