boost::bind参数例子使用
来源:互联网 发布:java 多态 编辑:程序博客网 时间:2024/06/13 21:42
bind并不是一个单独的类或函数,而是非常庞大的家族,依据绑定的参数个数和要绑定的调用对象类型,总共有十个不同的形式,但它们的名字都叫bind.
bind接受的第一个参数必须是一个可调用对象f,包括函数,函数指针,函数对象和成员函数,之后bind接受最多9个参数,参数的数量必须与f的参数数量相等
_1,_2这些一直可以到9,是占位符,必须在绑定表达式中提供函数要求的所有参数,无论是真实参数还是占位符均可以。占位符不可以超过函数参数数量。
绑定普通函数:
- #include<boost/bind.hpp>
- #include<iostream>
- using namespace std;
- using namespace boost;
- void fun(int a,int b){
- cout << a+b << endl;
- }
- int main()
- {
- bind(fun,1,2)();//fun(1,2)
- bind(fun,_1,_2)(1,2);//fun(1,2)
- bind(fun,_2,_1)(1,2);//fun(2,1)
- bind(fun,_2,_2)(1,2);//fun(2,2)
- bind(fun,_1,3)(1);//fun(1,3)
- }
- 3
- 3
- 3
- 4
- 4
#include<boost/bind.hpp>#include<iostream>using namespace std;using namespace boost;void fun(int a,int b){ cout << a+b << endl;}int main(){ bind(fun,1,2)();//fun(1,2) bind(fun,_1,_2)(1,2);//fun(1,2) bind(fun,_2,_1)(1,2);//fun(2,1) bind(fun,_2,_2)(1,2);//fun(2,2) bind(fun,_1,3)(1);//fun(1,3)}33344
绑定成员函数:
- #include<boost/bind.hpp>
- #include<iostream>
- #include<vector>
- #include<algorithm>
- using namespace boost;
- using namespace std;
- struct point
- {
- int x,y;
- point(int a=0,int b=0):x(a),y(b){}
- void print(){
- cout << "(" << x << "," << y << ")\n";
- }
- void setX(int a){
- cout << "setX:" << a << endl;
- }
- void setXY(int x,int y){
- cout << "setX:" << x << ",setY:" << y << endl;
- }
- void setXYZ(int x,int y,int z){
- cout << "setX:" << x << ",setY:" << y << "setZ:" << z << endl;
- }
- };
- int main()
- {
- point p1,p2;
- bind(&point::setX,p1,_1)(10);
- bind(&point::setXY,p1,_1,_2)(10,20);
- bind(&point::setXYZ,p2,_1,_2,_3)(10,20,30);
- vector<point> v(10);
- //for_each的时候只需要_1就可以了
- for_each(v.begin(),v.end(),bind(&point::print,_1));
- for_each(v.begin(),v.end(),bind(&point::setX,_1,10));
- for_each(v.begin(),v.end(),bind(&point::setXY,_1,10,20));
- for_each(v.begin(),v.end(),bind(&point::setXYZ,_1,10,20,30));
- }
- setX:10
- setX:10,setY:20
- setX:10,setY:20setZ:30
- (0,0)
- (0,0)
- (0,0)
- (0,0)
- (0,0)
- (0,0)
- (0,0)
- (0,0)
- (0,0)
- (0,0)
- setX:10
- setX:10
- setX:10
- setX:10
- setX:10
- setX:10
- setX:10
- setX:10
- setX:10
- setX:10
- setX:10,setY:20
- setX:10,setY:20
- setX:10,setY:20
- setX:10,setY:20
- setX:10,setY:20
- setX:10,setY:20
- setX:10,setY:20
- setX:10,setY:20
- setX:10,setY:20
- setX:10,setY:20
- setX:10,setY:20setZ:30
- setX:10,setY:20setZ:30
- setX:10,setY:20setZ:30
- setX:10,setY:20setZ:30
- setX:10,setY:20setZ:30
- setX:10,setY:20setZ:30
- setX:10,setY:20setZ:30
- setX:10,setY:20setZ:30
- setX:10,setY:20setZ:30
- setX:10,setY:20setZ:30
0 0
- boost::bind参数例子使用
- 【boost】boost::bind的使用
- BOOST::bind 如何使用
- boost::bind的使用
- boost bind的使用
- boost bind使用资料
- boost bind的使用
- Boost.bind 的使用
- boost::bind的使用
- 使用boost的bind
- Boost::bind使用详解
- boost::bind和boost::function使用示例
- boost::function与boost::bind 的使用
- boost的bind使用感受
- Boost.Bind的基础使用
- Boost.Bind的基础使用
- boost中bind的使用
- mem_fun和boost::Bind使用
- OpenGL--位图
- DPM(Defomable Parts Model) 源码分析-训练(三)
- 第一篇 为什么要写这个博客
- Android学习笔记之布局
- G.729A--编码--主函数
- boost::bind参数例子使用
- HDU 1081 To The Max
- 终于还是换到了这个新博客
- git 创建 .gitignore 文件 建立项目过滤规则
- SqlServer 更改复制代理配置文件参数及两种冲突策略设置
- Java源码分析之LinkedList
- 数据结构实验之栈四:括号匹配
- Could not create pool connection. The DBMS driver exception was: Io 异常: Broken pipe
- 移动前端开发之viewport的深入理解