四種函式配接器

2021-10-10 13:32:35 字數 1560 閱讀 2061

bind1nd

(op ,value)

//相當於構成op(value,param2),即用value覆蓋掉op的第乙個引數

bind2nd

(op ,value)

//相當於構成op(param1,value),即用value覆蓋掉op的第二個引數

2…對於接受乙個引數的仿函式op(param):

not1

(op)

//相當於構成!op(param),即op(param)的結果進行邏輯非運算

not2

(op)

//相當於構成!op(param,param),即把op(param,param)的結構進行邏輯非運算

classop:

public binary_functionbool

>

};

二、函式子類所繼承的基類unary_function或binary_function是stl提供的模板,需要先指定模板引數,規則是:

operator()的引數和返回型別中不含指標時,要省略const和&

classop:

public binary_functionbool

>

};

以指標為引數或返回型別,則把這個型別原樣抄;

classop:

public binary_function<

const t1*

,const t2*

,bool

>

};

三、對於普通函式f也需要用函式配接器結合時,需要先用ptr_fun(f)封裝,使普通函式變為仿函式才能用於配接器;

//下面例子中的普通函式fun,需先ptr_fun(fun)變成仿函式,才能使用配接器bind2nd與value值1結合,得到乙個新的仿函式bind2nd(ptr_fun(fun),1)

for_each

(b.begin()

, b.

end(),

bind2nd

(ptr_fun

(fun),1

));

#include

#include

#include

#include

#include

using

namespace std;

classa:

public binary_function<

int,

int,

void

>~a

()bool

operator()

(int a,

int b)

const

private:}

;void

fun(

int a,

int b)

intget

(int a)

intmain()

函式配接器

函式配接器 bind1st op,value op value,param 繫結op的第乙個引數 bind2st op,value op param,value not1 op op param not2 op op param1,param2 int func1 int p1,int p2 int...

c 函式配接器

所謂的 函式配接器 是指可以把仿函式和另乙個仿函式 或某個值,或某個一般函式 結合起來的仿函式。函式配接器也定義在標頭檔案 functional 中。include例如 finf if coll.begin coll.end range bind2nd reater,40 criterion其中的表...

函式,函式的四種定義,

函式 1.函式的關鍵字 function 複習var 是js的關鍵字,用於表示宣告變數,宣告是在記憶體模組完成,定義 是執行模組完成。var 客園在記憶體模組提前 js 執行前 完成所以有變數提公升這個功能。function 也是js1的關鍵字,用於宣告和定義函式的,宣告和定義都在記憶體模組完成。f...