生活随笔
收集整理的這篇文章主要介紹了
C++ 仿函数
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一,概述
? ? ? ? 仿函數(shù)(functor),就是使一個(gè)類(lèi)的使用看上去象一個(gè)函數(shù)。其實(shí)現(xiàn)就是類(lèi)中實(shí)現(xiàn)一個(gè)operator(),這個(gè)類(lèi)就有了類(lèi)似函數(shù)的行為,就是一個(gè)仿函數(shù)類(lèi)了。
有些功能的的代碼,會(huì)在不同的成員函數(shù)中用到,想復(fù)用這些代碼。
? ? ? ? ? ? ? ? ? ? ? ? ? ? 1)公共的函數(shù),可以,這是一個(gè)解決方法,不過(guò)函數(shù)用到的一些變量,就可能成為公共的全局變量,再說(shuō)為了復(fù)用這么一片代碼,就要單立出一個(gè)函數(shù),也不是很好維護(hù)。
? ? ? ? ? ? ? ? ? ? ? ? ? ? 2)仿函數(shù),寫(xiě)一個(gè)簡(jiǎn)單類(lèi),除了那些維護(hù)一個(gè)類(lèi)的成員函數(shù)外,就只是實(shí)現(xiàn)一個(gè)operator(),在類(lèi)實(shí)例化時(shí),就將要用的,非參數(shù)的元素傳入類(lèi)中。
二,仿函數(shù)(functor)在各編程語(yǔ)言中的應(yīng)用
1)C語(yǔ)言使用函數(shù)指針和回調(diào)函數(shù)來(lái)實(shí)現(xiàn)仿函數(shù),例如一個(gè)用來(lái)排序的函數(shù)可以這樣使用仿函數(shù)
[html]?view plaincopy
#include?<stdio.h>?? #include?<stdlib.h>?? //int?sort_function(?const?void?*a,?const?void?*b);?? int?sort_function(?const?void?*a,?const?void?*b)?? {????? ????return?*(int*)a-*(int*)b;?? }?? ?? int?main()?? {?? ????? ???int?list[5]?=?{?54,?21,?11,?67,?22?};?? ???qsort((void?*)list,?5,?sizeof(list[0]),?sort_function);//起始地址,個(gè)數(shù),元素大小,回調(diào)函數(shù)??? ???int??x;?? ???for?(x?=?0;?x?<?5;?x++)?? ??????????printf("%i\n",?list[x]);?? ???????????????????? ???return?0;?? }?? ? ? ? ? 2)在C++里,我們通過(guò)在一個(gè)類(lèi)中重載括號(hào)運(yùn)算符的方法使用一個(gè)函數(shù)對(duì)象而不是一個(gè)普通函數(shù)。
[html]?view plaincopy
#include?<iostream>?? #include?<algorithm>?? ?? using?namespace?std;?? template<typename?T>?? class?display?? {?? public:?? ????void?operator()(const?T?&x)?? ????{?? ????????cout<<x<<"?";??? ????}??? };??? ?? ?? int?main()?? {?? ????int?ia[]={1,2,3,4,5};?? ????for_each(ia,ia+5,display<int>());??? ?????? ????return?0;??? }???
三,仿函數(shù)在STL中的定義
? ? ? ? 要使用STL內(nèi)建的仿函數(shù),必須包含<functional>頭文件。而頭文件中包含的仿函數(shù)分類(lèi)包括
? ? ? ? ?1)算術(shù)類(lèi)仿函數(shù)
? ? ? ? ? ? ? ?加:plus<T>
? ? ? ? ? ? ? ?減:minus<T>
? ? ? ? ? ? ? ?乘:multiplies<T>
? ? ? ? ? ? ? ?除:divides<T>
? ? ? ? ? ? ? ?模取:modulus<T>
? ? ? ? ? ? ? ?否定:negate<T>
例子:
[html]?view plaincopy
#include?<iostream>?? #include?<numeric>?? #include?<vector>??? #include?<functional>??? using?namespace?std;?? ?? int?main()?? {?? ????int?ia[]={1,2,3,4,5};?? ????vector<int>?iv(ia,ia+5);?? ????cout<<accumulate(iv.begin(),iv.end(),1,multiplies<int>())<<endl;??? ?????? ????cout<<multiplies<int>()(3,5)<<endl;?? ?????? ????modulus<int>??modulusObj;?? ????cout<<modulusObj(3,5)<<endl;?//?3??? ????return?0;??? }???
? ? ? ? ?2)關(guān)系運(yùn)算類(lèi)仿函數(shù)
? ? ? ? ? ? ? ?等于:equal_to<T>
? ? ? ? ? ? ? ?不等于:not_equal_to<T>
? ? ? ? ? ? ? ?大于:greater<T>
? ? ? ? ? ? ? ?大于等于:greater_equal<T>
? ? ? ? ? ? ? ?小于:less<T>
? ? ? ? ? ? ? ?小于等于:less_equal<T>
? ? ? ? ? ? ? 從大到小排序:
[html]?view plaincopy
#include?<iostream>?? #include?<algorithm>?? #include?<vector>??? ?? using?namespace?std;?? ?? template?<class?T>??? class?display?? {?? public:?? ????void?operator()(const?T?&x)?? ????{?? ????????cout<<x<<"?";??? ????}??? };?? ?? int?main()?? {?? ????int?ia[]={1,5,4,3,2};?? ????vector<int>?iv(ia,ia+5);?? ????sort(iv.begin(),iv.end(),greater<int>());?? ????for_each(iv.begin(),iv.end(),display<int>());??? ????return?0;??? }??? ? ? ? ? ? ? 3)邏輯運(yùn)算仿函數(shù)
? ? ? ? ? ? ? ? ?邏輯與:logical_and<T>
? ? ? ? ? ? ? ? ?邏輯或:logical_or<T>
? ? ? ? ? ? ? ? ?邏輯否:logical_no<T>
總結(jié)
以上是生活随笔為你收集整理的C++ 仿函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。