C++11之function和bind
来源:互联网 发布:it 网络管理系统 价格 编辑:程序博客网 时间:2024/05/29 19:54
首先看看function的使用方法
#include <iostream>#include <functional>using namespace std;// 一般的函数void Func(){cout << __FUNCTION__ << endl;}class Test{public:// 类的静态函数static void TestFunc(int a){cout << __FUNCTION__ << endl;cout << a << endl;}};// 仿函数class Foo{public:void operator()(int a){cout << __FUNCTION__ << endl;cout << a << endl;}};int main(int arc,char** argv){Foo foo;// 定义两个function对象function<void()> fun1 = Func;function<void(int)> fun2 = Test::TestFunc;function<void(int)> fun3 = foo;// 像使用普通函数一样使用function对象fun1();fun2(10);fun3(20);return 0;}bind的使用方式
#include <iostream>#include <functional>using namespace std;// 一般的函数void Func(){cout << __FUNCTION__ << endl;}class Test{public:// 类的静态函数static void TestFunc(int a){cout << __FUNCTION__ << endl;cout << a << endl;}};// 仿函数class Foo{public:void operator()(int a,int b){cout << __FUNCTION__ << endl;cout << "a = "<< a << " b= " << b << endl;}void Output(int a){cout << __FUNCTION__ << endl;cout << a << endl;}};int main(int arc,char** argv){Foo foo;// bind的使用auto fun1 = bind(Func);auto fun2 = bind(Test::TestFunc, std::placeholders::_1); // std::placeholders::_1,std::placeholders::_2等是占位符,表示这个位置在函数被调用时,被传入的第1,2个参数所代替auto fun3 = bind(foo, 10,std::placeholders::_1);auto fun4 = bind(&Foo::Output, &foo, std::placeholders::_1);fun1();fun2(20); // 输出20fun3(30); // 输出 10,30fun4(40); // 输出 40return 0;}
0 0
- C++11之function和bind
- C++11 学习笔记 std::function和bind绑定器
- C++11 笔记 std::function,bind绑定器和lambda
- C++11之std::function和std::bind
- c++:使用function模板和bind函数
- [C++]C++11:Function与Bind
- C++11中的function和bind
- C++11中的function和bind
- c++11中的bind和function
- c++11:std::function和bind
- std::function 和 std::bind
- boost::function和boost::bind
- std::function 和 std::bind
- boost::function 和boost::bind
- C++11新特性之std::bind与std::function
- C++11新特性之std::function & std::bind
- C++11 FAQ中文版:std::function 和 std::bind
- C++11 FAQ中文版:std::function 和 std::bind
- Yii2-权限控制RBAC实战篇(一)
- 线程有哪些状态
- hdu 2084 数塔
- jQuery之#id选择器
- Batch File Redirection
- C++11之function和bind
- 程序员面试技巧
- shell编程注意事项
- muduo库的学习2--Socket的基本设计---代码细节
- 制作initrd(6):重做Ubuntu安装盘
- MatLab基础
- iOS 创建单例的两种方法
- fcntl函数详解
- Leetcode旅途三