C++——function和bind的一些应用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++——function和bind的一些应用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2758字,纯文字阅读大概需要4分钟。
内容图文
![C++——function和bind的一些应用](/upload/InfoBanner/zyjiaocheng/630/a3edfc4761194ad798038b915219e8c7.jpg)
function是一种类模板,重载了operator()函数调用操作符,所以每一个function类的对象都是一个函数对象。
我们可以这样使用function模板:
#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; }
在bind()函数中,我们首先传递要绑定的函数地址,然后根据我们重新定义的函数即function<void (Foo*, int)> pF2来确定我们的参数个数及顺序。
void foo(int i); function<void (Foo*, int)> pf2 = bind(&Foo::foo, std::placeholders::_1, std::placeholders::_2);
由于foo()函数是类成员函数,它有一个隐式参数,所以我们原函数含有两个参数。根据function模板,我们需要按照Foo *, int的顺序传递参数,在原函数中,
Foo *是第一个参数,对应放置的位置为1, int是第二个参数, 对应的位置为2, 这样,我们调用std::palceholders::来将参数的位置确定下来。
占位符_1、_2指的是实际函数调用实参的位置。
下面是一个有三个参数的函数对应的各种function可能,我们可以从中自己体会,也可以自己尝试重新实现以下。
代码如下:
#include <iostream> 2 #include <string> 3 #include <vector> 4 #include <functional> 5 using namespace std; 6 7 void test(int i, double d, const string &s) 8 { 9 cout << "i = " << i << " d = " << d << " s = " << s << endl; 10 } 11 int main(int argc, const char *argv[]) 12 { 13 function<void (int, double, const string&)> f1 = &test; 14 f1(12, 3.14, "foo"); 15 16 //1.void (*)(int, double) 17 function<void (int, double)> f2 = 18 std::bind(&test, std::placeholders::_1, std::placeholders::_2, "foo"); 19 f2(12, 3.14); 20 21 //2.void (*)(double, int, const string &) 22 function<void (double, int, const string &)> f3 = 23 std::bind(&test, std::placeholders::_2, std::placeholders::_1, std::placeholders::_3); 24 f3(3.14, 12, "foo"); 25 26 //3.void (*)(const string &, int) 27 function<void (const string &, int)> f4 = 28 std::bind(&test, std::placeholders::_2, 3.14, std::placeholders::_1); 29 f4("foo", 12); 30 31 //4. void (*) (const string &, int, double) 32 function<void (const string &, int, double)> f5 = 33 std::bind(&test, std::placeholders::_2, std::placeholders::_3, std::placeholders::_1); 34 f5("foo", 12, 3.14); 35 36 //5. void (*)(int) 37 function<void (int)> f6 = 38 std::bind(&test, std::placeholders::_1, 3.14, "foo"); 39 f6(12); 40 41 //6 void(*)(const string &) 42 function<void (const string &)> f7 = 43 std::bind(&test, 12, 3.14, std::placeholders::_1); 44 f7("foo"); 45 46 //7. void (*)() 47 function<void ()> f8 = 48 std::bind(&test, 12, 3.14, "foo"); 49 f8(); 50 }
转自:https://www.cnblogs.com/gjn135120/p/4015554.html
内容总结
以上是互联网集市为您收集整理的C++——function和bind的一些应用全部内容,希望文章能够帮你解决C++——function和bind的一些应用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。