std::binder1st, std::binder2nd
定義於標頭檔案 <functional> |
||
template< class Fn > class binder1st |
(1) | (C++11 起已棄用) (在 C++17 中已移除) |
template< class Fn > class binder2nd |
(2) | (C++11 起已棄用) (在 C++17 中已移除) |
將實參繫結到二元函式(binary function)的函式物件。
引數值在物件構造時傳入並存儲於物件內部。當函式物件透過 operator()
被呼叫時,儲存的值將作為其中一個實參,另一個實參作為 operator()
的實參傳入。結果函式物件為一元函式(unary function)。
[編輯] 示例
#include <cmath> #include <functional> #include <iostream> #include <vector> const double pi = std::acos(-1); // use std::numbers::pi in C++20 int main() { // deprecated in C++11, removed in C++17 auto f1 = std::bind1st(std::multiplies<double>(), pi / 180.0); // C++11 replacement auto f2 = [](double a) { return a * pi / 180.0; }; for (double n : {0, 30, 45, 60, 90, 180}) std::cout << n << "°\t" << std::fixed << "= " << f1(n) << " rad (using binder)\t= " << f2(n) << " rad (using lambda)\n" << std::defaultfloat; }
輸出
0° = 0.000000 rad (using binder) = 0.000000 rad (using lambda) 30° = 0.523599 rad (using binder) = 0.523599 rad (using lambda) 45° = 0.785398 rad (using binder) = 0.785398 rad (using lambda) 60° = 1.047198 rad (using binder) = 1.047198 rad (using lambda) 90° = 1.570796 rad (using binder) = 1.570796 rad (using lambda) 180° = 3.141593 rad (using binder) = 3.141593 rad (using lambda)
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 109 | C++98 | operator() 無法修改傳入的實參 |
添加了過載以處理此問題 |
[編輯] 參閱
(C++11 中已廢棄)(C++17 中已移除) |
將一個引數繫結到二元函式 (函式模板) |