日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL学习笔记(仿函数)

發(fā)布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL学习笔记(仿函数) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

仿函數(shù)(Functors)

仿函數(shù)(functor),就是使一個類的使用看上去象一個函數(shù)。其實現(xiàn)就是類中實現(xiàn)一個operator(),這個類就有了類似函數(shù)的行為,就是一個仿函數(shù)類了。

例如我們定義一個類:

class X{public:return-value operator()(arguments) const;... };

然后就可以把這個類別的對象當做函數(shù)調用

X fo; ... fo(arg1,arg2) //等價于fo.operator()(arg1,arg2);

?顯然,這種定義形式更為復雜,卻又三大妙處:

1.仿函數(shù)比一般函數(shù)更靈巧,因為它可以擁有狀態(tài)。

2.每個仿函數(shù)都有其型別。因此可以將仿函數(shù)的型別當做template參數(shù)傳遞。

3.執(zhí)行速度上,仿函數(shù)通常比函數(shù)指針更快。

?

?

仿函數(shù)可當做排序準則

1 #include <iostream> 2 #include <string> 3 #include <set> 4 #include <algorithm> 5 using namespace std; 6 7 class Person{ 8 public: 9 string firstname() const; 10 string lastname() const; 11 ... 12 }; 13 14 class PersonSortCriterion{ 15 public: 16 bool operator()(const Person&p1,const Person& p2) const { 17 return p1.lastname()<p2.lastname()|| 18 (!(p2.lastname()<p1.lastname())&& 19 p1.firstname()<p2.firstname()); 20 } 21 }; 22 23 int main() 24 { 25 typedef set<Person,PersonSortCriterion> PersonSet; 26 PersonSet coll; 27 PersonSet::iterator pos; 28 for(pos=coll.begin();pos!=coll.end();++pos){ 29 ... 30 } 31 ... 32 } View Code

這里的coll適用了特殊排序準則PersonSortCritersion,而它是一個仿函數(shù)類別。所以可以當做set的template參數(shù),而一般函數(shù)則無法做到這一點。

?

擁有內部狀態(tài)的仿函數(shù)

下面例子展示仿函數(shù)如何模擬函數(shù)在同一時刻下?lián)碛卸鄠€狀態(tài)

1 #include <iostream> 2 #include <list> 3 #include <algorithm> 4 #include "print.cpp" 5 using namespace std; 6 7 class IntSequence 8 { 9 private: 10 int value; 11 public: 12 IntSequence(int initialValue):value(initialValue){} 13 int operator() () 14 { 15 return value++; 16 } 17 }; 18 19 int main() 20 { 21 list<int> coll; 22 generate_n(back_inserter(coll),9,IntSequence(1)); 23 PRINT_ELEMENTS(coll); 24 generate(++coll.begin(),--coll.end(),IntSequence(42)); 25 PRINT_ELEMENTS(coll); 26 } View Code

?

for_each()的返回值

使用for_each()可以返回其仿函數(shù)。下面將演示這一點

1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 using namespace std; 5 6 class MeanValue 7 { 8 private: 9 long num; 10 long sum; 11 public: 12 MeanValue():num(0),sum(0){} 13 void operator() (int elem) 14 { 15 num++; 16 sum+=elem; 17 } 18 double value() 19 { 20 return static_cast<double>(sum)/static_cast<double>(num); 21 } 22 }; 23 24 int main() 25 { 26 vector<int> coll; 27 for(int i=1;i<=8;++i) 28 { 29 coll.push_back(i); 30 } 31 MeanValue mv=for_each(coll.begin(),coll.end(),MeanValue()); 32 cout<<"mean value:"<<mv.value()<<endl; 33 } View Code

?

預定義的仿函數(shù)

C++標準程序庫提供了許多預定義的仿函數(shù)。下面列出了所有這些仿函數(shù)

對對象排序或進行比較時,一般都以less<>為預設排序準則。要使用這些仿函數(shù),必須包含頭文件<functional>。

?

?

函數(shù)配接器(Function Adapters)

所謂“函數(shù)配接器”是指能夠將仿函數(shù)和另一個仿函數(shù)(或某個值,或一般函數(shù))結合起來的仿函數(shù)。函數(shù)配接器也聲明與<functional>中。

例如以下語句:

find_if(coll.begin(),coll.end(),bind2nd(greater<int>()),42)

其中bind2nd是將一個二元仿函數(shù)(greater<>)轉換成一元仿函數(shù)。它通常將第二參數(shù)傳給“由第一參數(shù)指出”的二元仿函數(shù),作為后者的第二參數(shù)。

下面列出了預定義的函數(shù)配接器

?

轉載于:https://www.cnblogs.com/runnyu/p/4840489.html

總結

以上是生活随笔為你收集整理的STL学习笔记(仿函数)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。