boost::bind

// 忽略警告
#define _SCL_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#include <assert.h>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/typeof/typeof.hpp>
#include <boost/assign/list_of.hpp>

using namespace boost;
using namespace std;

void bindFun1()
{
	cout << "触发绑定函数bindFun1()" << endl;
}

void bindFun2(int a, int c)
{
	cout << a << "    " << c << endl;
}

class CExample
{
public:
	CExample()
	{
		bBindPrintstr1 = boost::bind(&CExample::printInt, this, _1);// 类中绑定成员函数,参数1必须加上&CExample::,参数2传入this指针,参数3为站位符
	}
	bool printStr(const std::string &str)
	{
		std::cout << "CExample::printStr:" << str << std::endl;
		return true;
	}

	void printInt(int num)
	{
		std::cout << "CExample::printInt:" << num << std::endl;
	}
	boost::function< void(int) > bBindPrintstr1;
};

struct point
{
	int x, y;
	point(int a = 0, int b = 0) :x(a), y(b){}
	void print()
	{
		cout << x << "    " << y << endl;
	}
};


template<class _Ty = void>
struct com2 :public binary_function<_Ty, _Ty, _Ty>
{
	bool operator()(_Ty a, _Ty b)// 大于于比较
	{
		return a > b;
	}
};
int main()
{
	// 绑定函数
	boost::function<void()> pFun1 = boost::bind(&bindFun1);// 绑定binFun1 可以省略&
	boost::function<void(int, int)> pFun2 = &bindFun2;
	boost::function<void(int, int)> pFun22 = boost::bind(&bindFun2, _2, _1);// 参数顺序,第一个int用参数_1,第二个int用参数_2
	pFun1();// 触发绑定函数bindFun1()
	pFun2(1, 2); //1       2
	pFun22(1, 2);//2       1




	boost::function< bool(const std::string&) > bBindPrintstr;
	CExample example;
	bBindPrintstr = boost::bind(&CExample::printStr, &example, _1);// 类外绑定成员函数则传入类对象指针(也可以是对象),后续参数用站位符(_1)表示
	bBindPrintstr("hello world!");//CExample::printStr:hello world!
	example.bBindPrintstr1(1111);//CExample::printInt:1111

	vector<point> v;
	for (int i = 0; i < 10; ++i)
	{
		v.push_back(point( i, pow(i* 1.0, 2) ));
	}
	for_each(v.begin(), v.end(), boost::bind(&point::print, _1));

	// 绑定point的成员变量x
	vector<int> v2(v.size());
	transform(v.begin(), v.end(), v2.begin(), boost::bind(&point::x, _1));
	for (int it : v2)
	{
		cout << it << endl;
	}

	typedef pair<int, string> pair_t;
	pair_t p(123, "string");
	cout << boost::bind(&pair_t::first, p)() << endl;// 123


	// 绑定函数对象
	cout << bind(std::greater<int>(), _1, 0)(-1) << endl;	// 检查是否大于0    0
	boost::function< int(int, int) > pf = bind(plus<int>(), _1, _2);
	cout << pf(1, 2) << endl;								// 相加操作		3
	BOOST_AUTO(Parity, bind(modulus<int>(), _1, 2));		// 判断奇偶			1
	cout << Parity(9) << endl;

	// 绑定自定义函数对象
	struct com1
	{
		bool operator()(int a, int b)// 小于比较
		{
			return a < b;
		}
	};

	// 占位符别名
	const boost::arg<1> & arg_1 = boost::placeholders::_1;
	const boost::arg<2> & arg_2 = boost::placeholders::_2;

	auto c = bind<bool>(com1(), _1, _2);// 自定义类型(不具有result_type类型)必须在bind后面写上<返回值类型>
	cout << c(2, 1) << endl;// 0

	//com2 继承了binary_function(有result_type类型)可以省略<返回值类型>
	auto c1 = bind(com2<int>(), arg_1, arg_2);// 
	cout << c1(2, 1) << endl;// 1

	// f(g(x))的bind表达式为bind(f,bind(g,_1))(x)

	// 逻辑表达式
	remove_if(v.begin(), v.end(), bind(&point::x, _1) == 0);// 点(0,0) 被删除
	remove_if(v.begin(), v.end(), bind(&point::x, _1) == 1);// 点(1,1) 被删除
	assert(v[0].x == 2);
	BOOST_AUTO(pos, find_if(v.begin(), v.end(), bind(&point::x, _1) == 3 && bind(&point::y, _1) == 9));
	assert(pos != v.end());
	// 上述表达式等价
	struct pred{
		bool operator()(point &p)
		{
			return p.x == 3 && p.y == 9;
		}
	};
	BOOST_AUTO(pos1, find_if(v.begin(), v.end(), bind<bool>(pred(), _1)));
	assert(pos1 != v.end());
	// 绑定非标准函数
	bind<int>(printf, "%d + %d = %d\n", _1, _2, _3)(5, 6, 5 + 6);

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值