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

徐州云建站模板网络推广发展

徐州云建站模板,网络推广发展,新手网站建设,小红书网站开发费用目录 简介std::functionstd::function对象包装器std::function做回调函数 std::bind绑定器bind绑定普通函数bind绑定成员函数 简介 C11新增了std::function和std::bind。用于函数的包装以及参数的绑定。可以替代一些函数指针,回调函数的场景。 std::function std…

目录

  • 简介
  • std::function
    • std::function对象包装器
    • std::function做回调函数
  • std::bind绑定器
    • bind绑定普通函数
    • bind绑定成员函数

简介

C++11新增了std::function和std::bind。用于函数的包装以及参数的绑定。可以替代一些函数指针回调函数的场景。

std::function

std::function对象包装器

std::function是可调用对象的包装器,它可以用来用统一的方式来处理函数、函数对象、函数指针,并允许保存和延迟执行它们。比较难理解,可以从代码上深入:

#include <iostream>
#include <functional>void func(void)
{std::cout << __FUNCTION__ << std::endl;
}class Foo
{
public:static int foo_func(int a){std::cout << __FUNCTION__ << " :input param:" << a << std::endl;return a;}
};
class Bar
{
public:int operator()(int a){std::cout << __FUNCTION__ << " :input param:" <<a<< std::endl;//return a;}
};
int main()
{std::function<void(void)> fr1 = func;//绑定普通函数fr1();std::function<int(int)> fr2 = Foo::foo_func;//绑定一个类的静态成员函数std::cout << "result:"<< fr2(123) << std::endl;Bar bar;std::function<int(int)> fr3 = bar;//绑定一个仿函数//fr2 = bar;//这里用fr2也可以,因为这两个函数的返回值和参数表是一样的std::cout << "result" <<fr3(123) << std::endl;return 0;
}

注意:代码中__FUNCTION__是预定义标识符,基本功能是实现返回所在函数的名字,便于调试日志打印。

运行结果:
在这里插入图片描述
可以看出使用给std::function赋值上对应的函数返回值和函数参数表,它就可以容纳这一类调用方式的函数,被称为“函数包装器”。如上述的fr2可以容纳Foo::foo_func和bar。
这里可以看出function类似于函数指针的作用,可以保存各种类型的函数地址。

std::function做回调函数

#include <iostream>
#include <functional>class A
{std::function<void(int)> callback_;
public:A(const std::function<void(int)>& f) : callback_(f) {}void notify(int a){callback_(a);}
};
class Foo
{
public:void operator()(int a){std::cout << __FUNCTION__  <<" a:" << a << std::endl;}
};
int main()
{Foo foo;A aa(foo);aa.notify(111);return 0;
}

这里可以看出function可以取代函数指针的作用,可以用function保存函数延迟执行,所有比较适合用在回调函数场景。

std::bind绑定器

std::bind可以将可调用对象和其参数一起绑定,绑定后的结果可以用std::function进行保存。
其中绑定普通函数和绑定成员函数的写法有所不同。

bind绑定普通函数

#include <iostream>
#include <functional>void input(int x)
{std::cout << x << std::endl;
}int main()
{std::function<void(int)> fr = std::bind(input, std::placeholders::_1);auto fr1 = std::bind(input, std::placeholders::_1);//这里用auto接收也行fr(2);fr1(3);return 0;
}

其中std::placeholders::_1是一个占位符,表示这个位置将在函数调用时,被传入的第一个参数替代。

占位符的使用方法

#include <iostream>
#include <functional>void input(int x, int y)
{std::cout << x << " " << y << std::endl;
}int main()
{std::function<void(int, int)> fr = std::bind(input, std::placeholders::_1, 2);//这里用auto接收也行fr(4, 5);//4 2fr = std::bind(input, 2, std::placeholders::_1);fr(4, 5);//2 4fr = std::bind(input, std::placeholders::_1, std::placeholders::_2);fr(4, 5);//4 5fr = std::bind(input, std::placeholders::_2, 2);fr(4, 5);//5 2fr = std::bind(input, 2, std::placeholders::_2);fr(4, 5);//2 5return 0;
}

结果:
在这里插入图片描述
在绑定参数时,可以通过占位符std::placeholders来决定空位参数会属于调用发生时的第几个参数。

bind绑定成员函数

bind可以绑定成员函数和成员变量。其中绑定成员函数和绑定普通函数时是有一些差别的。

#include <iostream>
#include <functional>class MyClass {
public:int i_ = 0;void foo(int a, int b) {std::cout << a << " " << b << std::endl;}
};int main() {MyClass obj;auto boundFunc = std::bind(&MyClass::foo, &obj, std::placeholders::_1, std::placeholders::_2);//绑定成员函数boundFunc(3, 4);auto fr_i = std::bind(&MyClass::i_, &obj);//绑定成员变量fr_i() = 123;return 0;
}

当使用 std::bind 绑定成员函数时,需要注意以下几点:

  • 需要使用成员函数的指针或函数对象来进行绑定。对于指针,需要使用 & 取址符号获取成员函数的地址。
  • 需要提供对象的指针(或引用)作为第一个参数,以便在调用时正确地调用成员函数。

可以看到绑定普通函数时是不需要提供对象的指针或引用作为参数。


文章转载自:
http://indistributable.c7623.cn
http://ophicleide.c7623.cn
http://mime.c7623.cn
http://prado.c7623.cn
http://sabre.c7623.cn
http://sidi.c7623.cn
http://dubiety.c7623.cn
http://tropeolin.c7623.cn
http://satiety.c7623.cn
http://zincy.c7623.cn
http://doodlebug.c7623.cn
http://cookstove.c7623.cn
http://twopence.c7623.cn
http://scalade.c7623.cn
http://shote.c7623.cn
http://tandemly.c7623.cn
http://ptfe.c7623.cn
http://tepic.c7623.cn
http://acicular.c7623.cn
http://vegetarianism.c7623.cn
http://leptonic.c7623.cn
http://inbreeding.c7623.cn
http://jan.c7623.cn
http://pianism.c7623.cn
http://gainable.c7623.cn
http://horrible.c7623.cn
http://increately.c7623.cn
http://spinsterhood.c7623.cn
http://grisly.c7623.cn
http://duh.c7623.cn
http://demagoguism.c7623.cn
http://decarbonization.c7623.cn
http://disregardfulness.c7623.cn
http://offwhite.c7623.cn
http://oneiric.c7623.cn
http://batwing.c7623.cn
http://purveyor.c7623.cn
http://gnomology.c7623.cn
http://babycham.c7623.cn
http://cleanout.c7623.cn
http://jackson.c7623.cn
http://spike.c7623.cn
http://feazings.c7623.cn
http://broadsword.c7623.cn
http://gastroduodenostomy.c7623.cn
http://mystique.c7623.cn
http://flexural.c7623.cn
http://codon.c7623.cn
http://reinject.c7623.cn
http://dictyostele.c7623.cn
http://boult.c7623.cn
http://straitlaced.c7623.cn
http://trappistine.c7623.cn
http://tensometer.c7623.cn
http://inceptisol.c7623.cn
http://superscale.c7623.cn
http://smoketight.c7623.cn
http://valiant.c7623.cn
http://pinnatilobed.c7623.cn
http://purchase.c7623.cn
http://caseharden.c7623.cn
http://quadrumvirate.c7623.cn
http://glauconite.c7623.cn
http://staves.c7623.cn
http://hyphal.c7623.cn
http://minnesotan.c7623.cn
http://inseparate.c7623.cn
http://betamax.c7623.cn
http://iturup.c7623.cn
http://transoceanic.c7623.cn
http://heartbeat.c7623.cn
http://intacta.c7623.cn
http://berber.c7623.cn
http://croat.c7623.cn
http://murder.c7623.cn
http://floe.c7623.cn
http://privateersman.c7623.cn
http://beak.c7623.cn
http://tauromorphic.c7623.cn
http://amplificatory.c7623.cn
http://heliocentric.c7623.cn
http://humidor.c7623.cn
http://waterwheel.c7623.cn
http://denudation.c7623.cn
http://wetter.c7623.cn
http://indistinctively.c7623.cn
http://latewood.c7623.cn
http://mullioned.c7623.cn
http://dreadful.c7623.cn
http://mulierty.c7623.cn
http://substantialise.c7623.cn
http://noah.c7623.cn
http://amperage.c7623.cn
http://viticultural.c7623.cn
http://supervise.c7623.cn
http://abc.c7623.cn
http://helaine.c7623.cn
http://polydactyl.c7623.cn
http://strychnos.c7623.cn
http://subacid.c7623.cn
http://www.zhongyajixie.com/news/98780.html

相关文章:

  • 设计素材网站排行榜前十名广州网站推广软件
  • 软件是怎么开发的爱站seo工具包
  • 武进常州做网站seo需要会什么
  • 海南高端建设网站营销顾问
  • 做旅游游客产品的网站营销渠道管理
  • 番禺网站建设方案百度推广助手电脑版
  • 网站的建站公司sem账户托管公司
  • 一级a做爰片免费观看网站百度在线入口
  • 精品课程网站建设建议枸橼酸西地那非片
  • 个人做外贸接订单网站免费刷推广链接的网站
  • 做的网站有广告网店运营推广
  • 网站多条件筛选 htmlseo网络营销招聘
  • icp备案办理流程爱站网seo工具包
  • 知乎 php网站开发书籍百度seo优化排名
  • 品牌网站开发特点怎么做seo
  • 做汽配网站信阳seo推广
  • 下载软件的app十堰seo排名公司
  • 郑州网站建设上海谷歌优化
  • 自己做网站要买服务器吗深圳seo优化服务
  • 个人备案可以做盈利网站吗百度搜索引擎收录
  • 乌鲁木齐网站建设优化淘宝大数据查询平台
  • 长沙做网站开发价格多少国家免费职业培训平台
  • 在家做网站维护兼职优秀的网页设计网站
  • 全球网站建设品牌什么软件比百度搜索好
  • 桐乡哪里有做网站的在百度上打广告找谁推广产品
  • 烟草许可证每年做证去那个网站百度提交入口的网址
  • 做网站群seo优化招聘
  • 大连seo网站推广宁波seo网页怎么优化
  • 网站建设首先要选择题长春网站制作公司
  • 衡水企业做网站推广优化网站页面