`
soulwzy
  • 浏览: 14953 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
最近访客 更多访客>>
社区版块
存档分类
最新评论

Boost::bind 测试代码

 
阅读更多


#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));


}

 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics