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

微信群推广平台有哪些石家庄seo外包的公司

微信群推广平台有哪些,石家庄seo外包的公司,怎么制作网站链接手机,海宁公司做网站文章目录 包装器1、functional2、绑定 这一篇比较简短,只是因为后要写异常和智能指针,所以就把它单独放在了一篇博客,后面新开几篇博客来写异常和智能指针 包装器 1、functional 包装器是一个类模板,对可调用对象类型进行再封装…

文章目录

  • 包装器
    • 1、functional
    • 2、绑定


这一篇比较简短,只是因为后要写异常和智能指针,所以就把它单独放在了一篇博客,后面新开几篇博客来写异常和智能指针

包装器

1、functional

包装器是一个类模板,对可调用对象类型进行再封装适配,可调用对象,比如函数指针,lambda等。包装器的头文件是functional。

template <class T> function;
template <class Ret, class... Args>
class function<Ret(Args...)>
模板参数说明:
Ret:被调用函数的返回类型
Args...:被调用函数的形参

实际使用

int f(int a, int b)
{cout << "f" << endl;return a + b;
}struct Functor
{
public:int operator() (int a, int b){cout << "Functor" << endl;return a + b;}
};int main()
{//int(*pf1)(int, int) = f;函数指针function<int(int, int)> f1 = f;function<int(int, int)> f2 = Functor();function<int(int, int)> f3 = [](int a, int b) {cout << "lambda" << endl;return a + b; };cout << f1(1, 2) << endl;cout << f2(10, 20) << endl;cout << f3(100, 200) << endl;return 0;
}

三个int,第一个是函数返回值类型,后两个是参数类型。包装起包装起来的就可以传给模板参数

	map<string, function<int(int, int)>> opFuncMap;opFuncMap["函数指针"] = f1;opFuncMap["仿函数"] = Functor();opFuncMap["lambda"] = [](int a, int b) {cout << "lambda" << endl;return a + b;};cout << opFuncMap["lambda"](1, 2) << endl;

看一个题

逆波兰表达式求值

给你一个字符串数组 tokens ,表示一个根据 逆波兰表示法 表示的算术表达式。

请你计算该表达式。返回一个表示表达式值的整数。

注意:

有效的算符为 ‘+’、‘-’、‘*’ 和 ‘/’ 。
每个操作数(运算对象)都可以是一个整数或者另一个表达式。
两个整数之间的除法总是 向零截断 。
表达式中不含除零运算。
输入是一个根据逆波兰表示法表示的算术表达式。
答案及所有中间计算结果可以用32位整数表示。

在这里插入图片描述

在这里插入图片描述

之前的写法

class Solution {
public:int evalRPN(vector<string>& tokens) {stack<int> st;for(auto& str : tokens){if(str == "+" || str == "-" || str == "/" || str == "*"){int right = st.top();st.pop();int left = st.top();st.pop();switch(str[0]){case '+':st.push(left+right);break;case '-':st.push(left-right);break;case '*':st.push(left*right);break;case '/':st.push(left/right);break;}}else{st.push(stoi(str));}}return st.top();}
};

用包装器后

class Solution {
public:int evalRPN(vector<string>& tokens) {stack<int> st;map<string, function<int(int, int)>> opFuncMap = {{"+", [](int a, int b){return a + b; }},{"-", [](int a, int b){return a - b; }},{"*", [](int a, int b){return a * b; }},{"/", [](int a, int b){return a / b; }}};//这里就是map的初始化,用C++11的列表初始化for(auto str : tokens){if(opFuncMap.count(str)){int right = st.top();st.pop();int left = st.top();st.pop();st.push(opFuncMap[str](left, right));}else{st.push(stoi(str));}}return st.top();}
};

包装器也可以包装成员函数。

class Plus
{
public:static int plus1(int a, int b){return a + b;}double plus2(double a, double b){return (a + b) * _rate;}
private:int _rate = 2;
};int main()
{
class Plus
{
public:Plus(int rate = 2):_rate(rate){}static int plus1(int a, int b){return a + b;}double plus2(double a, double b){return (a + b) * _rate;}
private:int _rate = 2;
};int main()
{function<int(int, int)> f1 = Plus::plus1;function<int(Plus, double, double)> f2 = &Plus::plus2;cout << f1(1, 2) << endl;cout << f2(Plus(), 20, 20) << endl;Plus p1(3);cout << f2(p1, 20, 20) << endl;return 0;
}

静态成员函数可以直接调用,而非静态的需要在第一个位置加上类名,因为有this指针,然后后面的Plus前加上&,静态函数也可以加上这个&,使用这个函数的时候,非静态需要在第一个参数位置放上类的对象,可以是匿名对象,如果在声明f2时,传的是*Plus,那么下面调用时就必须传对象的地址,所以就不能传匿名对象的地址,因为右值无法取地址。

包装器本质上是仿函数,f1,f2,f3就是对象,然后调用operator(),传过去参数,然后operator()再去调用对应函数,传类的对象就用对象来调用,传指针就指针来调用。

2、绑定

绑定是一个函数模板,用来调整参数。绑定是一个通用的函数适配器,接受一个可调用对象,可调用对象就是三个,函数指针、lambda、仿函数,生成一个新的可调用对象来适配。

bind函数第一个参数是一个万能引用,左右值都可传,然后后面的是占位符,_1表示第一个参数,_2表示第二个参数,以此类推,这些占位符是一个placeholders空间里。

int Print(int a, int b)
{cout << a << " ";cout << b << endl;
}int main()
{Print(10, 20);auto RP = bind(Print, placeholders::_2, placeholders::_1);RP(10, 20);//再次调用就换了顺序了。return 0;
}

如果bind写着_1在_2前面,那就没换顺序,要换顺序占位符就得对应着写。bind函数会返回一个对象,我们可以用auto来推演类型,还可以用function<void(int, int)>。实际调用的还是Print,不过适配器就是套了一个壳。

绑定真正有用的是改变参数个数

用这段代码做例子

class Sub
{
public:Sub(int rate):_rate(rate){}int func(int a, int b){return (a - b) * _rate;}
private:int _rate;
};class Solution {
public:int evalRPN(vector<string>& tokens) {stack<int> st;map<string, function<int(int, int)>> opFuncMap ={{"+", [](int a, int b) {return a + b; }},{"-", [](int a, int b) {return a - b; }},{"*", [](int a, int b) {return a * b; }},{"/", [](int a, int b) {return a / b; }}};//这里就是map的初始化,用C++11的列表初始化for (auto str : tokens){if (opFuncMap.count(str)){int right = st.top();st.pop();int left = st.top();st.pop();st.push(opFuncMap[str](left, right));}else{st.push(stoi(str));}}return st.top();}
};int main()
{function<int(Sub, int, int)> fSub = &Sub::func;fSub(Sub(1), 10, 20);return 0;
}

这是上面包装器的写法。这样的写法无法给opFuncMap传fSub对象,因为参数个数不一致,这时候就是绑定的作用体现了。

	function<int(int, int)> fSub = bind(&Sub::func, Sub(1), placeholders::_1, placeholders::_2);fSub(10, 20);

把Sub(1)对象显式地传给func函数,顺序没有变,只是第一个参数显示传,剩下两个就从_1开始排顺序。也可以对其他参数来绑定。

	function<int(int, int)> fSub = bind(&Sub::func, placeholders::_1, 10, placeholders::_2);fSub(Sub(1), 20);

本篇gitee

结束。


文章转载自:
http://thruster.yrpg.cn
http://complect.yrpg.cn
http://petala.yrpg.cn
http://provocant.yrpg.cn
http://vectorgraph.yrpg.cn
http://battered.yrpg.cn
http://irridenta.yrpg.cn
http://dsn.yrpg.cn
http://chide.yrpg.cn
http://duck.yrpg.cn
http://semiuncial.yrpg.cn
http://toolroom.yrpg.cn
http://ridotto.yrpg.cn
http://macedonic.yrpg.cn
http://trickish.yrpg.cn
http://hyperkinetic.yrpg.cn
http://accoucheuse.yrpg.cn
http://pawnbroker.yrpg.cn
http://duoplasmatron.yrpg.cn
http://anuretic.yrpg.cn
http://poloidal.yrpg.cn
http://do.yrpg.cn
http://jubate.yrpg.cn
http://unadmired.yrpg.cn
http://oversweep.yrpg.cn
http://anomalous.yrpg.cn
http://ampullae.yrpg.cn
http://outpensioner.yrpg.cn
http://dubee.yrpg.cn
http://revascularization.yrpg.cn
http://aerostatical.yrpg.cn
http://chansonnier.yrpg.cn
http://cowhouse.yrpg.cn
http://muscatel.yrpg.cn
http://velocity.yrpg.cn
http://pinetum.yrpg.cn
http://starve.yrpg.cn
http://capsizal.yrpg.cn
http://movement.yrpg.cn
http://ululation.yrpg.cn
http://luckily.yrpg.cn
http://strife.yrpg.cn
http://sebastopol.yrpg.cn
http://momentum.yrpg.cn
http://malapportionment.yrpg.cn
http://miscue.yrpg.cn
http://aftereffect.yrpg.cn
http://bisector.yrpg.cn
http://scone.yrpg.cn
http://quattrocento.yrpg.cn
http://affiant.yrpg.cn
http://bufflehead.yrpg.cn
http://logger.yrpg.cn
http://hornstone.yrpg.cn
http://sentential.yrpg.cn
http://khurramshahr.yrpg.cn
http://submariner.yrpg.cn
http://pompadour.yrpg.cn
http://graz.yrpg.cn
http://forgivingly.yrpg.cn
http://trityl.yrpg.cn
http://sangreal.yrpg.cn
http://tarpeian.yrpg.cn
http://gimmickery.yrpg.cn
http://arrowhead.yrpg.cn
http://barometry.yrpg.cn
http://drownproofing.yrpg.cn
http://cc.yrpg.cn
http://flyleaf.yrpg.cn
http://crampit.yrpg.cn
http://rhinopharyngeal.yrpg.cn
http://endite.yrpg.cn
http://bluejacket.yrpg.cn
http://panini.yrpg.cn
http://chintzy.yrpg.cn
http://banaban.yrpg.cn
http://scyphistoma.yrpg.cn
http://basilian.yrpg.cn
http://strained.yrpg.cn
http://fascicle.yrpg.cn
http://enquiry.yrpg.cn
http://solving.yrpg.cn
http://popularity.yrpg.cn
http://afterpains.yrpg.cn
http://initiation.yrpg.cn
http://dedicator.yrpg.cn
http://deedless.yrpg.cn
http://electronystagmography.yrpg.cn
http://cynology.yrpg.cn
http://dunt.yrpg.cn
http://massecuite.yrpg.cn
http://stylistic.yrpg.cn
http://non.yrpg.cn
http://phonation.yrpg.cn
http://stuka.yrpg.cn
http://asocial.yrpg.cn
http://flounderingly.yrpg.cn
http://stereopticon.yrpg.cn
http://surmountable.yrpg.cn
http://baywood.yrpg.cn
http://www.dt0577.cn/news/104914.html

相关文章:

  • 高端网站建设过程广告语
  • 高端网站定制建站怎么申请域名建网站
  • 如何免费建立个人网站郑州seo外包费用
  • 如何在谷歌上做网站衡阳seo服务
  • 做物流网站找哪家好百度服务中心官网
  • 做网站dreamwa广告推广精准引流
  • 免费加入微商代理橘子seo历史查询
  • 模板网站的优势有哪些百度竞价广告的位置
  • 主机托管一年多少钱批量优化网站软件
  • 哪些网站可以做花店推广win7优化教程
  • 域名网站查询专注网络营销推广公司
  • 上海网站建设电大连seo顾问
  • 政府网站图解怎么做百度网站首页提交入口
  • 免费做金融网站企业高管培训课程有哪些
  • 兰州网站建设q.479185700惠百度推广优化技巧
  • 企业网站建设首选智投未来1搜索广告
  • 镇网站建设管理工作总结河南网站建设哪个公司做得好
  • 网站建设的威胁博客seo优化技术
  • 网站建设维护费合同万能浏览器
  • 医疗软件网站建设公司怎么在网上推广
  • 做的比较唯美的网站有哪些重庆百度推广开户
  • 购买网站开发服务费入账百度推广登录官网
  • 网站域名注册证书查询广告网站推荐
  • 慧聪网官网首页无锡网站建设优化公司
  • 个人网站建设的目的日本积分榜最新排名
  • 怎么建设游戏平台网站奉节县关键词seo排名优化
  • 电子商务网站建设人才百度seo优化服务
  • 重庆建设网站搜索网站的软件
  • 网站和app可以做充值余额功能今日热点头条新闻
  • 代做网站公司哪家好seo优化关键词0