boost::bind参数例子使用
2015-04-14 19:36
190 查看
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>
#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
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) } 3 3 3 4 4
绑定成员函数:
#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
相关文章推荐
- Boost关于bind的使用以及函数对象和传递参数的问题
- boost::bind 与 boost::function 的使用方法例子
- VS 2005环境下使用boost ::bind的一个注意事项
- PowerShell函数参数使用智能提示功能例子
- 一个类里可以注册不同类型的消息及boost:function和boost:bind的使用
- SpringMVC 在业务控制方法中写入模型变量收集参数,且使用@InitBind来解决字符串转日期类型
- JS中使用Array函数shift和pop创建可忽略参数的例子
- ASP.net(c#) 在ACCESS数据库中利用参数使用存储过程例子(防SQL注入)
- Oracle游标、参数的使用例子
- boost::bind的理解与使用
- boost::bind的使用
- window.open的例子和使用方法以及参数说明(完整版)
- boost::bind的使用方法
- boost::bind的几种使用
- CRM:AttachEvent带参数的使用例子
- window.open的例子和使用方法以及参数说明(完整版)
- 【Boost】boost库中function和bind一起使用的技巧(二)
- 【C++】boost::bind和函数对象一起使用实现便捷的异步编程
- boost的库中bind和function使用实例
- nginx rewrite 伪静态配置参数和使用例子