函数适配器
来源:互联网 发布:阿里数据包导入淘宝 编辑:程序博客网 时间:2024/05/16 05:45
#include <iostream>#include <string>#include <vector>#include <functional>using namespace std;void foo(const string &s){ cout << s << endl;}int main(int argc, const char *argv[]){ void (*pFunc) (const string &) = &foo; pFunc("bar"); function<void (const string&)> f = &foo; f("bar"); return 0;}
#include <iostream>#include <string>#include <vector>#include <functional>using namespace std;void foo(int i, double d){ cout << i << d << endl;}int main(int argc, const char *argv[]){ function<void (int, double)> f = &foo; f(12, 4.5); return 0;}
#include <iostream>#include <string>#include <vector>#include <functional>using namespace std;class Foo{ public: void foo(int i) { cout << i << endl; } static void bar(double d) { cout << d << endl; }};int main(int argc, const char *argv[]){ //mem_fun void (*)(Foo *, int) Foo f; (mem_fun(&Foo::foo))(&f, 123); //把成员函数转换成函数对象 function<void (int)> pf = bind(&Foo::foo, &f, std::placeholders::_1); pf(345); function<void (Foo*, int)> pf2 = bind(&Foo::foo, std::placeholders::_1, std::placeholders::_2);//数字代表原函数第几个参数 pf2(&f, 456); function<void (int, Foo*)> pf3 = bind(&Foo::foo, std::placeholders::_2, std::placeholders::_1); pf3(567, &f); return 0;}
#include <iostream>#include <string>#include <vector>#include <functional>using namespace std;using namespace std::placeholders;void test(int i, double d, const string &s){ cout << "i = " << i << " d = " << d << " s = " << s << endl;}int main(int argc, const char *argv[]){ function<void (int, double, const string&)> f1 = &test; f1(12, 3.14, "foo"); //1.void (*)(int, double) function<void (int, double)> f2 = std::bind(&test, _1, _2, "foo"); //2.void (*)(double, int, const string &) function<void (double, int, const string &)> f3 = std::bind(&test, _2, _1, _3); //3.void (*)(const string &, int) function<void (const string &, int)> f4 = std::bind(&test, _2, 3.4, _1); //4. void (*) (const string &, int, double) function<void (const string&, int, double)> f5 = std::bind(&test, _2, _3, _1); //5. void (*)(int) function<void (int)> f6 = bind(&test, _1, 3.4, "bar"); //6 void(*)(const string &) function<void (const string &)> f7 = bind(&test, 12, 4.5, _1); //7. void (*)() function<void()> f8 = bind(&test, 12, 4.5, "bar");}
0 0
- 函数适配器
- 函数适配器
- 函数适配器
- 函数适配器
- 函数适配器
- 函数适配器
- 函数适配器
- 函数适配器
- 函数适配器
- 仿函数 函数适配器
- 常规函数适配器,成员函数适配器
- 函数对象及适配器
- 成员函数线程适配器
- STL 函数适配器
- C++函数适配器
- STL 函数适配器
- 【STL】函数适配器
- 函数适配器bind2nd
- 两个分析查询的有用函数
- JavaScript权威指南学习之第6章 对象
- DirectX11 光照与材质的相互作用
- 模式匹配(第一篇
- BZOJ 3530 数数【AC自动机+数位dp】
- 函数适配器
- 第4周—项目2 建立“单链表”算法库
- 文章标题
- 从面向过程到面向对象
- Python学习笔记<文件操作>
- nsbundle的一些获取本地信息
- 记录一个实用的可以进行网站分析的网站netcraft
- 安卓控件使用系列17:ImageView获取网络图片
- Ubuntu 虚拟机网络问题