STL学习笔记(仿函数)
仿函數(shù)(Functors)
仿函數(shù)(functor),就是使一個(gè)類的使用看上去象一個(gè)函數(shù)。其實(shí)現(xiàn)就是類中實(shí)現(xiàn)一個(gè)operator(),這個(gè)類就有了類似函數(shù)的行為,就是一個(gè)仿函數(shù)類了。
例如我們定義一個(gè)類:
class X{public:return-value operator()(arguments) const;... };然后就可以把這個(gè)類別的對(duì)象當(dāng)做函數(shù)調(diào)用
X fo; ... fo(arg1,arg2) //等價(jià)于fo.operator()(arg1,arg2);?顯然,這種定義形式更為復(fù)雜,卻又三大妙處:
1.仿函數(shù)比一般函數(shù)更靈巧,因?yàn)樗梢該碛袪顟B(tài)。
2.每個(gè)仿函數(shù)都有其型別。因此可以將仿函數(shù)的型別當(dāng)做template參數(shù)傳遞。
3.執(zhí)行速度上,仿函數(shù)通常比函數(shù)指針更快。
?
?
仿函數(shù)可當(dāng)做排序準(zhǔn)則
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適用了特殊排序準(zhǔn)則PersonSortCritersion,而它是一個(gè)仿函數(shù)類別。所以可以當(dāng)做set的template參數(shù),而一般函數(shù)則無法做到這一點(diǎn)。
?
擁有內(nèi)部狀態(tài)的仿函數(shù)
下面例子展示仿函數(shù)如何模擬函數(shù)在同一時(shí)刻下?lián)碛卸鄠€(gè)狀態(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ù)。下面將演示這一點(diǎn)
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?
預(yù)定義的仿函數(shù)
C++標(biāo)準(zhǔn)程序庫提供了許多預(yù)定義的仿函數(shù)。下面列出了所有這些仿函數(shù)
對(duì)對(duì)象排序或進(jìn)行比較時(shí),一般都以less<>為預(yù)設(shè)排序準(zhǔn)則。要使用這些仿函數(shù),必須包含頭文件<functional>。
?
?
函數(shù)配接器(Function Adapters)
所謂“函數(shù)配接器”是指能夠?qū)⒎潞瘮?shù)和另一個(gè)仿函數(shù)(或某個(gè)值,或一般函數(shù))結(jié)合起來的仿函數(shù)。函數(shù)配接器也聲明與<functional>中。
例如以下語句:
find_if(coll.begin(),coll.end(),bind2nd(greater<int>()),42)其中bind2nd是將一個(gè)二元仿函數(shù)(greater<>)轉(zhuǎn)換成一元仿函數(shù)。它通常將第二參數(shù)傳給“由第一參數(shù)指出”的二元仿函數(shù),作為后者的第二參數(shù)。
下面列出了預(yù)定義的函數(shù)配接器
?
轉(zhuǎn)載于:https://www.cnblogs.com/runnyu/p/4840489.html
總結(jié)
以上是生活随笔為你收集整理的STL学习笔记(仿函数)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 双人五子棋(C++游戏)游戏代码在最底下
- 下一篇: mac mysql客户端工具 知乎_Ma