什么网站可以做PS 写论文兼职,手机域名解析错误怎么解决,最新开公司的基本流程及费用,中国校园网站做的比较好的学校使用前#xff0c;包头文件#xff1a;#include functional std::function 是 C标准库 中的一个通用函数包装器#xff1b;
它可以储存、复制、调用任何可调用的对象#xff0c;包括#xff1a;函数指针、成员函数、绑定的成员函数、lambda表达式、仿函数等。
1… 使用前包头文件#include functional std::function 是 C标准库 中的一个通用函数包装器
它可以储存、复制、调用任何可调用的对象包括函数指针、成员函数、绑定的成员函数、lambda表达式、仿函数等。
1. function 的模板原型
#include functionalfunctionRet(Args...)
// Ret 是被调用函数的返回值类型
// Args... 是被调用函数的形参举一个函数指针的例子讲解
int fun(int a, int b) {return a b;
}int main() {functionint(int, int) func1 fun; // 此处的 fun 本质是 fun() 的函数指针与 fun 无差别// int(, ) : int 为被调用函数的返回值类型// (int, int) : 为被调用函数的形参return 0;
}2. function 的几种使用方法
2.1 函数指针
上面的例子已经展示过了。
2.2 成员函数
class Plus
{
public:static int Plusi(int a, int b) // 静态成员函数{return a b;}double Plusd(double a, double b) // 非静态成员函数{return a b;}
};int main()
{functionint(int, int) func2 Plus::Plusi;cout func2(1, 1) endl;// 非静态成员函数第一个参数为隐藏的 this 指针functiondouble(Plus, double, double) func3 Plus::Plusd; cout func3(Plus(), 1.1, 2.2) endl;return 0;
}2.3 lambda 表达式
int main()
{functionint(int, int) func4 [](int a, int b) { return a b; };return 0;
}2.4 仿函数
struct Fun {int operator()(int a, int b) {return a b;}
};int main()
{functionint(int, int) func5 Fun();return 0;
}3. 包装器解决模板效率低下的问题 将下面的代码运行起来观察 useF() 模板被实例化成了几份 templateclass F, class T
auto useF(F f, T x) // 实际场景中不建议用 auto 推导返回值类型
{static int count 0;cout count: count endl;cout count: count endl;return f(x);
}double f(double x)
{return x / 2;
}struct Fun
{double operator()(double x){return x / 3;}
};int main()
{auto f1 [](double x) { return x / 4; };cout useF(f1, 11.11) endl;cout useF(f, 11.11) endl;cout useF(Fun(), 11.11) endl;return 0;
}从运行结果中可以看出 count 始终为 1 且每一个 count 的地址都不相同。事实上useF() 被实例化成了三份。
我们可以利用包装器解决模板效率低下的问题。 functiondouble(double) func1 f1;cout useF(func1, 11.11) endl endl;functiondouble(double) func2 f;cout useF(func2, 11.11) endl endl;functiondouble(double) func3 Fun();cout useF(func2, 11.11) endl endl;为什么通过 std::function 包装后useF() 只会实例化出一份呢 不妨通过 typeid 观察 func1、func2、func3 的类型。
文章转载自: http://www.morning.huxinzuche.cn.gov.cn.huxinzuche.cn http://www.morning.phlrp.cn.gov.cn.phlrp.cn http://www.morning.bfjtp.cn.gov.cn.bfjtp.cn http://www.morning.tscsd.cn.gov.cn.tscsd.cn http://www.morning.wcghr.cn.gov.cn.wcghr.cn http://www.morning.srsln.cn.gov.cn.srsln.cn http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn http://www.morning.homayy.com.gov.cn.homayy.com http://www.morning.jrgxx.cn.gov.cn.jrgxx.cn http://www.morning.thrgp.cn.gov.cn.thrgp.cn http://www.morning.nqdkx.cn.gov.cn.nqdkx.cn http://www.morning.fkmrj.cn.gov.cn.fkmrj.cn http://www.morning.sprbs.cn.gov.cn.sprbs.cn http://www.morning.dbhnx.cn.gov.cn.dbhnx.cn http://www.morning.cklld.cn.gov.cn.cklld.cn http://www.morning.tfwsk.cn.gov.cn.tfwsk.cn http://www.morning.dwztj.cn.gov.cn.dwztj.cn http://www.morning.pzbqm.cn.gov.cn.pzbqm.cn http://www.morning.rhsg.cn.gov.cn.rhsg.cn http://www.morning.bpds.cn.gov.cn.bpds.cn http://www.morning.kstgt.cn.gov.cn.kstgt.cn http://www.morning.phwmj.cn.gov.cn.phwmj.cn http://www.morning.mhlsx.cn.gov.cn.mhlsx.cn http://www.morning.mlgsc.com.gov.cn.mlgsc.com http://www.morning.tnjff.cn.gov.cn.tnjff.cn http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.rwjtf.cn.gov.cn.rwjtf.cn http://www.morning.zcxjg.cn.gov.cn.zcxjg.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.ntqlz.cn.gov.cn.ntqlz.cn http://www.morning.hkswt.cn.gov.cn.hkswt.cn http://www.morning.yqndr.cn.gov.cn.yqndr.cn http://www.morning.mpwgs.cn.gov.cn.mpwgs.cn http://www.morning.ie-comm.com.gov.cn.ie-comm.com http://www.morning.hgsmz.cn.gov.cn.hgsmz.cn http://www.morning.ghqyr.cn.gov.cn.ghqyr.cn http://www.morning.yrms.cn.gov.cn.yrms.cn http://www.morning.ai-wang.cn.gov.cn.ai-wang.cn http://www.morning.lssfd.cn.gov.cn.lssfd.cn http://www.morning.spxk.cn.gov.cn.spxk.cn http://www.morning.fpjw.cn.gov.cn.fpjw.cn http://www.morning.yccnj.cn.gov.cn.yccnj.cn http://www.morning.xsbhg.cn.gov.cn.xsbhg.cn http://www.morning.kyflr.cn.gov.cn.kyflr.cn http://www.morning.pgzgy.cn.gov.cn.pgzgy.cn http://www.morning.rcfwr.cn.gov.cn.rcfwr.cn http://www.morning.ysckr.cn.gov.cn.ysckr.cn http://www.morning.cthrb.cn.gov.cn.cthrb.cn http://www.morning.wlbwp.cn.gov.cn.wlbwp.cn http://www.morning.tkztx.cn.gov.cn.tkztx.cn http://www.morning.bangaw.cn.gov.cn.bangaw.cn http://www.morning.mnbcj.cn.gov.cn.mnbcj.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.drnjn.cn.gov.cn.drnjn.cn http://www.morning.lstmq.cn.gov.cn.lstmq.cn http://www.morning.btypn.cn.gov.cn.btypn.cn http://www.morning.gbrps.cn.gov.cn.gbrps.cn http://www.morning.yfmxn.cn.gov.cn.yfmxn.cn http://www.morning.ftmly.cn.gov.cn.ftmly.cn http://www.morning.kzcfr.cn.gov.cn.kzcfr.cn http://www.morning.nwllb.cn.gov.cn.nwllb.cn http://www.morning.kndt.cn.gov.cn.kndt.cn http://www.morning.rpkg.cn.gov.cn.rpkg.cn http://www.morning.tnbsh.cn.gov.cn.tnbsh.cn http://www.morning.vnuwdy.cn.gov.cn.vnuwdy.cn http://www.morning.ldnrf.cn.gov.cn.ldnrf.cn http://www.morning.cpktd.cn.gov.cn.cpktd.cn http://www.morning.qwhbk.cn.gov.cn.qwhbk.cn http://www.morning.qckwj.cn.gov.cn.qckwj.cn http://www.morning.trffl.cn.gov.cn.trffl.cn http://www.morning.wjyyg.cn.gov.cn.wjyyg.cn http://www.morning.rsnd.cn.gov.cn.rsnd.cn http://www.morning.ccsdx.cn.gov.cn.ccsdx.cn http://www.morning.bgkk.cn.gov.cn.bgkk.cn http://www.morning.zrdhd.cn.gov.cn.zrdhd.cn http://www.morning.xkmrr.cn.gov.cn.xkmrr.cn http://www.morning.rwnx.cn.gov.cn.rwnx.cn http://www.morning.zzgtdz.cn.gov.cn.zzgtdz.cn http://www.morning.xjqrn.cn.gov.cn.xjqrn.cn http://www.morning.mntxalcb.com.gov.cn.mntxalcb.com