1 函数对象函数对象就是重载了函数调用运算符operator()的类的对象。因为它的对象可以像普通函数一样被调用所以被称为“仿函数”。函数对象在 STL 中有三大核心优势可以携带状态最核心的优势。编译期多态性能极高。可以作为算法的“自定义策略”。1.1 函数对象使用示例示例#includeiostream#includestringclass myAdd{public:intoperator()(intdata1,intdata2){returndata1data2;}};class myPrint{public:myPrint(){mCount0;}voidoperator()(std::string msg){this-mCount;std::coutmsg: msgstd::endl;}// 记录调用次数intmCount;};voiddoPrint(myPrintp,std::string msg){p(msg);}intmain(){intsum0;myAdd madd;summadd(10,10);std::coutsum: sumstd::endl;// 记录调用次数即有自己的状态myPrint mp;mp(hello world);mp(hello world);std::coutmp count: mp.mCountstd::endl;// 作为参数传递myPrint mp2;doPrint(mp2,test mp2);system(pause);return0;}打印结果sum:20msg:hello world msg:hello world mp count:2msg:test mp2 请按任意键继续...2 谓词2.1 谓词概念返回bool类型的仿函数称为谓词。如果operator()接受一个参数那么叫做一元谓词接受两个参数叫做二元谓词。2.2 谓词使用示例示例#includeiostream#includevector#includestring#includealgorithmclass GreaterFive{public:booloperator()(intdata){if(data5){returntrue;}else{returnfalse;}}};class MyCompare{public:booloperator()(intdata1,intdata2){returndata1data2;}};intmain(){// 一元谓词使用示例std::vectorintv;for(inti0;i10;i){v.push_back(i);}// 查找容器中有没有大于5的数字// 使用匿名函数对象std::vectorint::iterator iterstd::find_if(v.begin(),v.end(),GreaterFive());if(iterv.end()){std::coutnot find .std::endl;}else{std::cout*iter: *iterstd::endl;}// 二元谓词使用示例std::vectorintv2;v2.push_back(50);v2.push_back(200);v2.push_back(32);v2.push_back(1);v2.push_back(999);// 进行排序默认为升序std::sort(v2.begin(),v2.end());for(std::vectorint::iterator iterv2.begin();iter!v2.end();iter){std::cout*iter ;}std::coutstd::endl;// 使用仿函数进行降序排列std::sort(v2.begin(),v2.end(),MyCompare());for(std::vectorint::iterator iterv2.begin();iter!v2.end();iter){std::cout*iter ;}std::coutstd::endl;system(pause);return0;}打印结果*iter: 6 1 32 50 200 999 999 200 50 32 1 请按任意键继续. . .3 内建函数对象3.1 内建函数对象概念STL中内建了一些函数对象可分为算术仿函数、关系仿函数和逻辑仿函数。这些仿函数所产生的对象用法和一般函数完全相同。3.2 算数仿函数实现四则运算。其中negate是一元运算其他都是二元运算。仿函数原型templateclass TT plusT// 加法仿函数templateclass TT minusT// 减法仿函数templateclass TT multipliesT// 乘法仿函数templateclass TT dividesT// 除法仿函数templateclass TT modulusT// 取模仿函数templateclass TT negateT// 取反仿函数使用示例#includeiostream#includestring#includealgorithm#includefunctionalintmain(){// 一元仿函数std::negateintdata1;std::coutdata1: data1(12)std::endl;// 二元仿函数std::plusintdata2;std::coutdata2: data2(20,24)std::endl;system(pause);return0;}打印结果data1: -12 data2: 44 请按任意键继续. . .3.3 关系仿函数实现关系对比。仿函数原型templateclass Tbool equal_toT// 等于templateclass Tbool not_equal_toT// 不等于templateclass Tbool greaterT// 大于templateclass Tbool greater_equalT//大于等于templateclass Tbool lessT// 小于templateclass Tbool less_equalT// 小于等于使用示例#includeiostream#includestring#includealgorithm#includefunctional#includevectorintmain(){std::vectorintv;v.push_back(50);v.push_back(200);v.push_back(32);v.push_back(1);v.push_back(999);// 进行排序默认为升序std::sort(v.begin(),v.end());for(std::vectorint::iterator iterv.begin();iter!v.end();iter){std::cout*iter ;}std::coutstd::endl;// 使用关系仿函数进行降序操作std::sort(v.begin(),v.end(),std::greaterint());for(std::vectorint::iterator iterv.begin();iter!v.end();iter){std::cout*iter ;}std::coutstd::endl;system(pause);return0;}打印结果1 32 50 200 999 999 200 50 32 1 请按任意键继续. . .3.4 逻辑仿函数实现逻辑运算。函数原型templateclass Tbool logical_andT// 逻辑与templateclass Tbool logical_orT// 逻辑或templateclass Tbool logical_notT// 逻辑非使用示例#includeiostream#includestring#includealgorithm#includefunctional#includevectorintmain(){std::vectorboolv;v.push_back(true);v.push_back(false);v.push_back(true);v.push_back(true);for(std::vectorbool::iterator iterv.begin();iter!v.end();iter){std::cout*iter ;}std::coutstd::endl;std::vectorboolv2;v2.resize(v.size());// 搬运v1容器数据并且利用逻辑非仿函数对数据取反std::transform(v.begin(),v.end(),v2.begin(),std::logical_notbool());for(std::vectorbool::iterator iterv2.begin();iter!v2.end();iter){std::cout*iter ;}std::coutstd::endl;system(pause);return0;}打印结果#include iostream #include string #include algorithm #include functional #include vector int main() { std::vectorbool v; v.push_back(true); v.push_back(false); v.push_back(true); v.push_back(true); for (std::vectorbool::iterator iter v.begin(); iter ! v.end(); iter) { std::cout *iter ; } std::cout std::endl; std::vectorbool v2; v2.resize(v.size()); // 搬运v1容器数据并且利用逻辑非仿函数对数据取反 std::transform(v.begin(), v.end(), v2.begin(), std::logical_notbool()); for (std::vectorbool::iterator iter v2.begin(); iter ! v2.end(); iter) { std::cout *iter ; } std::cout std::endl; system(pause); return 0; }