std::bind1st, std::bind2nd
来自 cppreference.cn
定义于头文件 <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 的第一个或第二个参数传递。
目录 |
[编辑] 参数
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); // 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) |
按顺序将可变数量的参数绑定到函数对象 (函数模板) |