std::bind1st, std::bind2nd
来自cppreference.com
                    
                                        
                    < cpp | utility | functional
                    
                                                            
                    |   在标头  <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 的第一参数到 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 的参数
 | 
返回值
包装 f 和 x 的函数对象。
异常
可能会抛出由实现定义的异常。
示例
运行此代码
#include <iostream> #include <iomanip> #include <algorithm> #include <functional> #include <cmath> #include <cstddef> #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.)); // 等价的 lambda 为: [pi](double a){ return a*pi / 180.; }); 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)  | 
  按顺序绑定一定数量的参数到函数对象  (函数模板)  |