boost::bind的几种使用

1. boost::bind与函数对象的结合使用:
有这样一个函数对象:
template <typename _Ty>
struct  iStrLess : public std::binary_function<_Ty, _Ty, bool>
{
public:
	bool operator () (const _Ty& left, const _Ty& right) const
	{
		_Ty ileft = boost::to_upper_copy(left);
		_Ty iright = boost::to_upper_copy(right);
	       return (ileft < iright);
	}
};

当与boost::bind结合使用时的一个案例:
typedef StringUtility::iStrEqual<std::string> iStrEqual_t;
iStrEqual_t strComp;
if (std::find_if(paramNames.begin(), paramNames.end(),
	boost::bind(&iStrEqual_t::operator(), &strComp, colName, _1))
		== paramNames.end())
{
	// to do something
}

2. boost::bind与STL的functor的结合使用:
a) 与std::logical_not的结合
		BOOST_AUTO(itFound, std::find_if(cellmarkers.begin(), cellmarkers.end(),
					boost::bind<bool>(std::logical_not<bool>(), boost::bind(&SomeClass::containedIn, _1, boost::cref(cellbox)))));
		if (itFound != cellmarkers.end()) return true;

等价于下面的代码段:
for (BOOST_AUTO(it, cellmarkers.begin()); it != cellmarkers.end(); ++it)
{
    if (!it->containedIn(cellbox)) return true;
}

b) 与std::equal_to的结合
std::vector<SomeClass>::iterator it = std::find_if(vObjs.begin(), vObjs.end(),
		boost::bind<bool>(std::equal_to<int>(), cellsetID,
		boost::bind(&SomeClass::GetClipSetId, _1)
		)
		);

等价于下面的代码段:
for (BOOST_AUTO(it, vObjs.begin()); it != vObjs.end(); ++it)
{
    if (it->GetClipSetId() == cellsetID)
    {
        // do something
        break;
    }
}

注意:外部的boost::bind需要标明返回参数,内部的就不需要。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值