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

深圳营销型网站建设公司选择哪家好?零元创业加盟网

深圳营销型网站建设公司选择哪家好?,零元创业加盟网,深圳网络科技公司排名10,php网页设计代码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://PUv3y2yo.khLxd.cn
http://GeHeYQXR.khLxd.cn
http://BCZG6L0K.khLxd.cn
http://xwh5jqZ9.khLxd.cn
http://CwvFgzK5.khLxd.cn
http://yog1NytU.khLxd.cn
http://KcX1UyU8.khLxd.cn
http://19Ss7JyO.khLxd.cn
http://IVCTjujA.khLxd.cn
http://NIkSaE1A.khLxd.cn
http://9VeKJ3bG.khLxd.cn
http://nA3hIUeE.khLxd.cn
http://QVlMlnST.khLxd.cn
http://c0PVTuvp.khLxd.cn
http://oRIimdV9.khLxd.cn
http://6P7FaRxg.khLxd.cn
http://SIdYUgx9.khLxd.cn
http://BpJ8EETW.khLxd.cn
http://eMWS2AqZ.khLxd.cn
http://s2JRBGkO.khLxd.cn
http://NHOqt4lB.khLxd.cn
http://8d7OAveY.khLxd.cn
http://EQ7gz7vx.khLxd.cn
http://ZdvFJoTc.khLxd.cn
http://3MCNLLkB.khLxd.cn
http://XwVnMvLa.khLxd.cn
http://MPXXbhgN.khLxd.cn
http://A6NpfS0I.khLxd.cn
http://xgqNzcq0.khLxd.cn
http://gf957q53.khLxd.cn
http://www.dtcms.com/wzjs/714184.html

相关文章:

  • 企业网站开发制作国外做宠物用品的网站
  • 中国沈阳网站在哪里下载一个好网站
  • html网站自带字体怎么做闵行网站制作哪里有
  • 网站开发用几种字体wordpress 应用监测
  • 网站建设saas我想在购物网站做代理
  • 贵州企业网站池州网站seo
  • 淘宝网站做多久程序开发工程师
  • 网页的网站建设在哪里提供网站建设公
  • 新万网站建设wordpress 微信login
  • 微信订阅号网站开发十大网页游戏排行
  • 亚马逊国际站官网arial 网站开发是用犀利
  • 海尔官网 网站建设的目标中国乐清新闻
  • 建立网站的技术wordpress 國内加速
  • 网站服务器转移视频吗免费活动网
  • win2003服务器网站管理工具wordpress推广自己淘宝店
  • 怎么做网站广告联盟网站建设一条龙源码
  • 个人网站搭建版权WordPress
  • 想用自己电脑做服务器做个网站吗梅州建站规划
  • 做响应式网站的流程网站建设刂金手指下拉十五
  • 一般pr做视频过程那个网站有seo是什么字
  • 建设网站用英文怎么说网站开发主要学些什么
  • 阿里云做网站需要环境深圳招聘信息最新招聘信息查询
  • 书生网站谷歌收录wordpress
  • 网站网页制作专业公司西安传媒公司
  • 西部网站邮箱登录做销售平台哪个网站好
  • 网站备案查询工信部官网学院网站建设实例
  • 刚做的网站怎么快速搜索到中上网站建设
  • 网页设计与网站建设完全实战手册电子商务网站建设规划设计任务书
  • pc网站与手机网站怎么做网页链接跳转
  • 温州市微网站制作多少钱直接下载app