概念:
本质:
函数对象使用:
#include#includeusing namespace std; //函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值 class myAdd { public: int operator()(int v1, int v2) { return v1 + v2; } }; void test1() { myAdd a1; cout << a1(1, 2) << endl; } //函数对象超出普通函数的概念,函数对象可以有自己的状态 class myPrint { public: myPrint() { this->count = 0; } void operator()(string str) { cout << str << endl; count++; } int count; }; void test2() { myPrint p1; p1("A"); p1("A"); p1("A"); cout << p1.count << endl; } //函数对象可以作为参数传递 void doPrint(myPrint& mp, string str) { mp(str); } void test3() { myPrint mp; doPrint(mp, "hello"); } int main() { test1(); test2(); test3(); return 0; }
#include#include#includeusing namespace std; class GreaterFive { public: bool operator()(int val) { return val > 5; } }; int main() { vectorv; for (int i = 0; i < 10; i++) { v.push_back(i); } //查找容器中大于5的元素 //GreaterFive() 匿名函数对象 vector::iterator it=find_if(v.begin(), v.end(), GreaterFive()); cout << *it << endl; return 0; }
#include#include#includeusing namespace std; class myCompare { public: bool operator()(int v1, int v2) { return v1 > v2; } }; int main() { vectorv; v.push_back(10); v.push_back(30); v.push_back(20); v.push_back(40); //默认sort升序排序 sort(v.begin(), v.end()); for (vector::iterator it = v.begin(); it != v.end(); it++) { cout << *it << " "; } cout << endl; sort(v.begin(), v.end(), myCompare()); for (vector::iterator it = v.begin(); it != v.end(); it++) { cout << *it << " "; } return 0; }
STL内建了一些函数对象:
用法:
其中negate是一元运算,其他都是二元运算:
templateT puls
//加法templateT minus
//减法templateT multiplies
//乘法templateT modulus
//除法templateT negate
//取反#include#includeusing namespace std; int main() { //取反 negaten; cout << n(50) << endl;//-50 cout << n(0) << endl;//0 //加法 只填一个模板参数,默认相同类型 plusp; cout << p(10, 20) << endl; return 0; }
templatebool equal_to
//等于templatebool not_equal_to
//不等于templatebool greater
//大于templatebool greater_equal
//大于等于templatebool less
//小于templatebool less_equal
//小于等于sort默认的排序规则是从小到大,底层默认使用了less
#include#include#include#includeusing namespace std; int main() { vectorv; v.push_back(10); v.push_back(30); v.push_back(40); v.push_back(20); for (vector::iterator it = v.begin(); it != v.end(); it++) { cout << *it << endl; } sort(v.begin(), v.end(), less()); for (vector::iterator it = v.begin(); it != v.end(); it++) { cout << *it << endl; } return 0; }
templatebool logical_and
逻辑与templatebool logical_or
逻辑或templatebool logical_not
逻辑非#include#include#include#includeusing namespace std; int main() { vectorv; v.push_back(true); v.push_back(false); v.push_back(true); v.push_back(false); for (vector::iterator it = v.begin(); it != v.end(); it++) { cout << *it << " "; } cout << endl; //将容器v搬运到容器v1中,并执行取反操作 vectorv2; v2.resize(v.size()); //注意没有 v2.end() transform(v.begin(), v.end(), v2.begin(), logical_not()); for (vector::iterator it = v2.begin(); it != v2.end(); it++) { cout << *it << " "; } return 0; }
Copyright © 2023 leiyu.cn. All Rights Reserved. 磊宇云计算 版权所有 许可证编号:B1-20233142/B2-20230630 山东磊宇云计算有限公司 鲁ICP备2020045424号
磊宇云计算致力于以最 “绿色节能” 的方式,让每一位上云的客户成为全球绿色节能和降低碳排放的贡献者