名稱空間
變體
操作

std::bind1st, std::bind2nd

來自 cppreference.com
 
 
 
函式物件
函式呼叫
(C++17)(C++23)
恆等函式物件
(C++20)
透明運算子包裝器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

舊繫結器和介面卡
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
bind1stbind2nd
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(直到 C++20*)
(直到 C++20*)
 
定義於標頭檔案 <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 的引數

[編輯] 返回值

一個函式物件,包裝 fx

[編輯] 異常

可能丟擲實現定義的異常。

[編輯] 示例

#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)
按順序將可變數量的引數繫結到函式物件
(函式模板) [編輯]