函式物件和函式介面卡

2021-09-10 17:47:01 字數 3772 閱讀 6643

用到以下演算法:

templatetype accumulate(inputiterator first, inputiterator last, type val, binaryfunction binaryop);

#include #include //包含數值演算法標頭檔案

using namespace std;

//定義乙個普通函式

int mult(int x, int y) ;

int main() ;

const int n = sizeof(a) / sizeof(int);

cout << "the result by multipling all elements in a is "

<< accumulate(a, a + n, 1, mult)

<< endl;

return 0;

}

//10_14.cpp

#include #include //包含數值演算法標頭檔案

using namespace std;

class multclass

};int main() ;

const int n = sizeof(a) / sizeof(int);

cout << "the result by multipling all elements in a is "

<< accumulate(a, a + n, 1, multclass()) //將類multclass傳遞給通用演算法

<< endl;

return 0;

}

用於關係運算、邏輯運算的函式物件(要求返回值為bool)

//10_15.cpp

#include #include //包含數值演算法標頭檔案

#include //包含標準函式物件標頭檔案

using namespace std;

int main() ;

const int n = sizeof(a) / sizeof(int);

cout << "the result by multipling all elements in a is 「

<< accumulate(a, a + n, 1, multiplies())

<< endl; //將標準函式物件傳遞給通用演算法

return 0;

}

// 10_16.cpp

#include #include#include#includeusing namespace std;

int main() ;

const int n = sizeof(intarr) / sizeof(int);

vectora(intarr, intarr + n);

cout << "before sorting:" << endl;

copy(a.begin(),a.end(),ostream_iterator(cout,"\t"));

cout << endl;

sort(a.begin(), a.end(), greater());

cout << "after sorting:" << endl;

copy(a.begin(),a.end(),ostream_iterator(cout,"\t"));

cout << endl;

return 0;

}

組合介面卡:not1、not2

函式指標介面卡:ptr_fun

成員函式介面卡:ptrfun、ptrfun_ref

//10_17.cpp

#include #include#include#includeusing namespace std;

int main() ;

const int n = sizeof(intarr) / sizeof(int);

vectora(intarr, intarr + n);

vector::iterator p = find_if(a.begin(), a.end(), bind2nd(greater(), 40));

if (p == a.end())

cout << "no element greater than 40" << endl;

else

cout << "first element greater than 40 is: " << *p << endl;

return 0;}注:

find_if演算法在stl中的原型宣告為:

templateinputiterator find_if(inputiterator first, inputiterator last, unarypredicate pred);

它的功能是查詢陣列[first, last)區間中第乙個pred(x)為真的元素。

// 10_18.cpp

#include #include#include#includeusing namespace std;

bool g(int x, int y)

int main() ;

const int n = sizeof(intarr) / sizeof(int);

vectora(intarr, intarr + n);

vector::iterator p;

p = find_if(a.begin(), a.end(), bind2nd(ptr_fun(g), 40));

if (p == a.end())

cout << "no element greater than 40" << endl;

else

cout << "first element greater than 40 is: " << *p << endl;

p = find_if(a.begin(), a.end(), not1(bind2nd(greater(), 15)));

if (p == a.end())

cout << "no element is not greater than 15" << endl;

else

cout << "first element that is not greater than 15 is: " << *p << endl;

p = find_if(a.begin(), a.end(), bind2nd(not2(greater()), 15));

if (p == a.end())

cout << "no element is not greater than 15" << endl;

else

cout << "first element that is not greater than 15 is: " << *p << endl;

return 0;

}

//10_19.cpp

#include #include #include #include using namespace std;

struct car

void display() const

};int main()

函式物件介面卡

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

函式物件的函式介面卡

函式物件的函式介面卡 標準庫還提供了一組函式介面卡用來特殊化或者擴充套件一元和二元函式物件介面卡是 一種特殊的類它被分成下面兩類 1 繫結器binder binder 通過把二元函式物件的乙個實參繫結到乙個特殊的值上 將其轉換成一元函式物件例如為了計數乙個容器中小於或等於10 的元素的個數我們可 能...

函式物件及介面卡

定義了呼叫操作符的類,其物件常稱作函式物件 function object 即它們的行為表現出類似於函式的行為。函式物件通常用作泛型演算法的實參,如標準庫中大量泛型演算法有需要函式物件的版本。函式物件使用起來可以比函式靈活。標準庫在標頭檔案標頭檔案中定義了一組算術 關係與邏輯函式物件類,還定義了一組...