STL之介面卡

2022-08-02 07:09:09 字數 1549 閱讀 9738

摘要:本文主要講了介面卡的一些內容,重要的是了解介面卡使用的步驟。

1 #include2 #include3 #include 4 #include5 #include 67

using

namespace

std;89

//第一步 繫結 資料 bind2nd

10//

繼承類 binary_function《引數型別1 ,引數型別2 ,返回值型別》

11//

加const修飾 operator()

12class myprint:public binary_function

17};

18void

test01()

24 cout << "

請輸入起始值

"<25int

num;

26 cin >>num;

27for_each(v.begin(), v.end(),bind2nd(myprint(),num)); 28}

2930

class greaterthenfive :public unary_function

3137

};38

39void

test02()

45 vector::iterator pos =find_if(v.begin(), v.end(), not1(greaterthenfive()));

46//

vector::iterator pos = find_if(v.begin(),v.end(),not1(bind2nd(greater(),5)));

//取反介面卡,找到的是小於5的數字

47if (pos!=v.end())

4851

else54}

5556

void myprint03(int v, int

start)

5760

//函式指標介面卡

61void

test03()

6268

//將函式指標 適配為 函式物件

69//

ptr_fun

70 for_each(v.begin(), v.end(), bind2nd(ptr_fun(myprint03), 100

));71}72

73//

成員函式介面卡

74class

person

7582

83void

showperson()

8487

void

plusage()

8891

92string

m_name;

93int

m_age;

94};

9596

void myprintperson(person &p)

97100

101void

test04()

102121

122int

main()

STL之介面卡

注意 需包含標頭檔案 functional 步驟 bind2nd 或者 bind1st 將兩個引數進行繫結 bind2nd 繫結順序是一致 類繼承 binary function 型別1 型別2 返回值型別 加 const 寫法 class myprint public binary functio...

STL之介面卡

摘要 本文主要講了介面卡的一些內容,重要的是了解介面卡使用的步驟。1 include2 include3 include 4 include5 include 67 using namespace std 89 第一步 繫結 資料 bind2nd 10 繼承類 binary function 引數型...

STL介面卡 函式介面卡

有時候需要對內建函式物件返回值進行進一步的簡單計算,或者填上多餘的引數,不能直接代入演算法。函式介面卡實現了這一功能,函式介面卡是將一種函式物件轉化為另一種符合要求的函式物件。函式介面卡可以分為4個大類 繫結介面卡 組合介面卡 指標函式介面卡和成員函式介面卡。需求 在遍歷容器的時候,將容器中的值全部...