函式物件介面卡 bind2nd bind1st

2021-06-20 21:07:26 字數 952 閱讀 6401

bind1st bind2nd為stl標準庫中函式,表示將二元函式物件轉化為一元函式物件。 如

//bind2nd(op, value) (param)相當於op(param, value)

cout << count_if(v.begin(), v.end(), bind2nd(modulus(),2)) << endl; 

表示將二元函式物件modulus轉化為一元函式物件,bind2nd 內部儲存第二個引數2,並作為modulus的第二個引數。

//bind1st(op, value)(param)相當於op(value, param);

cout << count_if(v.begin(), v.end(), bind1st(less(), 2)) << endl;

2作為lind1st 的第乙個引數。

針對成員函式的函式介面卡

**示例

class person

void print() const ;

void foo(const vector& v)

void foo2(const vector& v)

int main(void)

mem_fun_ref(op) 呼叫op,op是某物件的乙個const成員函式

mem_fun(op)        呼叫op,op是某物件指標的乙個const成員函式

針對一般函式的函式介面卡 示例

it = find_if(v.begin(), v.end(), bind2nd(ptr_fun(strcmp), ""));

查詢v中第乙個不等於「」的物件。使用ptr_fun將一般函式轉化為bind2nd可以接受的標準型別。

bool check(int elem)

it = find_if(v.begin(), v.end(), not1(ptr_fun(check)));

查詢 v中第乙個不小於3的物件。

函式物件介面卡

include include include include include include include using namespace std 函式物件介面卡bind1st,bind2nd struct myprint public binary function binary functi...

介面卡模式(類介面卡 物件介面卡)

做個筆記 引用 public inte ce usb public inte ce psp public class usber implements usb 類介面卡 psp適用usb介面 public class usbadapter extends usber implements psp 物...

介面卡模式 預設介面卡,類介面卡,物件介面卡

模式思想 改變乙個類的對外介面 增加或減少 以滿足不同外部呼叫者的需求 角色成員 目標介面 target 客戶所期待的介面。目標可以是具體的或抽象的類,也可以是介面。需要適配的類 adaptee 需要適配的類或適配者類。介面卡 adapter 通過包裝乙個需要適配的物件,把原介面轉換成目標介面。適配...