当前位置: 首页 > wzjs >正文

有什么兼职做it的网站好做网站的无锡

有什么兼职做it的网站好,做网站的无锡,wordpress 在哪里注册,谷歌搜索官网1.函数对象 1.1 概念 重载函数调用操作符的类,其对象被称为函数对象 函数对象使用重载的()时,行为类似函数调用,也成为仿函数 本质:函数对象(仿函数)是一个类,不是一…

1.函数对象

1.1 概念

重载函数调用操作符的类,其对象被称为函数对象

函数对象使用重载的()时,行为类似函数调用,也成为仿函数

本质:函数对象(仿函数)是一个类,不是一个函数

1.2 函数对象的使用

在使用时可以像普通函数一样调用,可以有参数和返回值

函数对象可以有自己的状态

函数对象可以作为参数传递

#include <iostream>
#include <vector>using namespace std;class Myadd{public:int operator()(int x,int y){return x+y;}
};void test1(){Myadd ma;cout<<ma(10,10)<<endl;
}//可以有自己的状态
class MyPrint{public: MyPrint(){count=0;}void operator()(string test){cout<<test<<endl;count++;}int count;
};void test2(){MyPrint mp;mp("hello world");mp("hello world");mp("hello world");cout<<"调用了"<<mp.count<<"次"<<endl;
}// 可以作为参数传递
void DoPrint(MyPrint &mp,string test){mp(test);
}void test3(){MyPrint mp;DoPrint(mp,"hello world");
}int main()
{test1();test2();test3();system("pause");return 0;
}

 2.谓词

2.1 概念:

返回bool类型的仿函数称为谓词

如果operator()接收一个参数成为一元谓词

如果operator()接收两个参数成为二元谓词

2.2 一元谓词

一元谓词(Unary Predicate) 是一个接受一个参数并返回布尔值(true 或 false 的函数对象或函数。
其核心作用是:对单个输入值进行条件判断,返回是否满足特定条件。

#include <iostream>
#include <vector>
#include<algorithm>using namespace std;struct CreateFive{bool operator()(int x){return x>5;}
};void test(){vector<int> v;for(int i=0;i<10;i++){v.push_back(i);}vector<int>::iterator it = find_if(v.begin(),v.end(),CreateFive());// 查找满足CreateFive的元素,指向第一个满足条件的位置if (it==v.end()){cout<<"没有大于5的元素"<<endl;}else{cout<<*it<<endl;//只输出一个6}
}int main()
{test();system("pause");return 0;
}

2.3 二元谓词

二元谓词(Binary Predicate) 是一个接受两个参数并返回布尔值(true 或 false 的函数对象或函数。
其核心作用是:对两个输入值进行条件判断,返回是否满足特定关系。

#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;class MyCompare{public:bool operator()(int v1,int v2){return v1>v2;}// 这个函数接受两个参数(v1 和 v2),返回一个布尔值(true 或 false),// 表示 v1 是否大于 v2。
};void test(){vector<int> v;v.push_back(10);v.push_back(20);v.push_back(30);v.push_back(40);v.push_back(50);sort(v.begin(),v.end());for(vector<int>::iterator it=v.begin();it!=v.end();it++){cout<<*it<<" ";   }cout<<endl;//改变排序顺序sort(v.begin(),v.end(),MyCompare());for(vector<int>::iterator it=v.begin();it!=v.end();it++){cout<<*it<<" ";   }cout<<endl;}int main()
{test();system("pause");return 0;
}

3.内建函数对象

3.1意义

STL内建了一些函数对象

需要在头文件#include<functional>

3.2算数仿函数

negate是一元运算,其他是二元运算,所以需要重载()

template<class T> T plus<T>;//加法

template<class T> T minus<T>;//减法

template<class T> T multiplies<T>;//乘法

template<class T> T divides<T>;//除法

template<class T> T modulus<T>;//取余

template<class T> T negate<T>;//取反

#include <iostream>
#include <functional>using namespace std;//negate
void Negate(){negate<int> n;cout<<n(10)<<endl;
}void Add(){plus<int> p;cout<<p(10,20)<<endl;
}int main()
{Negate();Add();system("pause");return 0;
}

3.3关系仿函数

实现关系对比

template<class T> bool equal_to<T>// 等号

template<class T> bool not_equal_to<T>// 不等号

template<class T> bool greater<T>// 大于

template<class T> bool less<T>// 小于

template<class T> bool greater_equal<T>// 大于等于

template<class T> bool less_equal<T>// 小于等于

#include <iostream>
#include <vector>
#include<functional>
#include<algorithm>using namespace std;class MyCompare{public:bool operator()(int x,int y){return x>y;}
};void test(){vector<int> v;v.push_back(10);v.push_back(80);v.push_back(30);v.push_back(50);v.push_back(70);for(vector<int>::iterator it=v.begin();it!=v.end();it++){cout<<*it<<" ";}cout<<endl;sort(v.begin(),v.end(),MyCompare());//STL内建仿函数sort(v.begin(),v.end(),greater<int>());for(vector<int>::iterator it=v.begin();it!=v.end();it++){cout<<*it<<" ";}cout<<endl;
}int main()
{test();system("pause");return 0;
}

3.4逻辑仿函数

template<class T> bool logical_and<T>// 逻辑与

template<class T> bool logical_or<T>// 逻辑或

template<class T> bool logical_not<T>// 逻辑非

#include <iostream>
#include <vector>
#include<functional>
#include<algorithm>using namespace std;void test(){vector<bool> v;v.push_back(true);v.push_back(false);v.push_back(true);v.push_back(false);for(auto it=v.begin();it!=v.end();it++){cout<<*it<<" ";}cout<<endl;vector<bool> v2;v2.resize(v.size());//将v2的大小设置为v的大小transform(v.begin(),v.end(),v2.begin(),logical_not<bool>());//遍历v,从v2的begin开始,将v的值取反,然后赋值给v2for(auto it=v2.begin();it!=v2.end();it++){cout<<*it<<" ";}cout<<endl;
}int main()
{test();system("pause");return 0;
}

http://www.dtcms.com/wzjs/562730.html

相关文章:

  • 网站建设时间表网页设计案例分析ppt
  • 优秀的个人网站设计模板dede网站源码
  • 电子商务网站的设计与实现网站备案会检查空间
  • 房产交易网站开发个人建网站怎么赚钱
  • 沧州网站seo公司wordpress更新配置
  • 一站式网页设计服务平台有什么做兼职的网站比较好
  • 建网站需要多少钱和什么条件有关如何评价网站是否做的好坏
  • 网站建设上线流程图wordpress打开置顶文章没用
  • 网站续费通知单wordpress 招聘主题
  • 在公司平台做网站竞拍wordpress主题翻译
  • 成都网站制作方案为什么我的电脑打开了第一个网站打开第二个网站就网络出问题了?
  • 天津网站建设网站的设计原则
  • 网站建设 我们的优势免费注册域名网站推荐
  • 网站视频播放器用什么做的大连金普新区规划建设局网站
  • 广州制作公司网站做鼻翼整形整形的网站
  • 关于建设校园网站申请wordpress编辑器软件
  • 秦皇岛建设厅网站网站开发怎么开发
  • app推广全国代理加盟seo优化排名教程百度技术
  • 百度网络优化推广公司天津网站的优化
  • 一个网站有多少网页深圳建设网站过程
  • 网页在线制作网站wordpress单设备登录
  • 如何解析后用二级域名做网站厚瑜珠海网站建设
  • 英语网站建设医疗软件网站建设
  • 国外做的比较好的网站商洛网站建设求职简历
  • 做网站公司需要什么条件重庆网站建设jccit
  • 盘县 网站建设网页设计与制作dw
  • 西安网站建设推广线上推广员的工作内容
  • 做网站用哪几个端口 比较好杭州公司网站制作维护
  • 网站开发文档步骤应该怎么写办宽带要多少钱
  • 网站配置服务Wordpress云南网站建设500