上面那種是functions,下面這種叫做functors.(我姑且翻譯成函子)兩者最本質(zhì)的區(qū)別在于,上面僅僅是一個(gè)過(guò)程;而下面,卻可以包含狀態(tài)。后者,可以輕松實(shí)現(xiàn)閉包。在C++11里面,后者直接演化為lambda了。我就用你提到的sort來(lái)舉一個(gè)小例子:cppboolmyfunction(inti,intj){return(istructmyclass{booloperator()(inti,intj){return(i}myobject;std::vectormyvector{32,71,12,45,26,80,53,33};std::sort(myvector.begin(),myvector.end(),myfunction);std::sort(myvector.begin(),myvector.end(),myobject);簡(jiǎn)化了你的例子,我們來(lái)關(guān)注本質(zhì)區(qū)別??雌饋?lái),好像等效對(duì)不?那么現(xiàn)在需求變了,排序的時(shí)候,我只希望排值大于40的元素,請(qǐng)問(wèn)咋整,你說(shuō),只好把這個(gè)40寫(xiě)到函數(shù)里了。那如果我說(shuō)這個(gè)40是來(lái)自用戶輸入呢?也可能是50或是60,請(qǐng)問(wèn)怎么辦?此時(shí),function好像有點(diǎn)沒(méi)有用武之地了。但我們的functor卻依然可以大顯身手。cppstructmyclass{intflag;myclass(inti):flag(i){}booloperator()(inti,intj){return((flag};std::vectormyvector{32,71,12,45,26,80,53,33};myclassmyobject(40);std::sort(myvector.begin(),myvector.end(),myobject);//output:3212263345537180例子可能有點(diǎn)怪。。但你明白這意思了么?