C++ _Find_Pr()算法解析

_Find_pr()算法比较简单,它的作用是将容器中的值与传入值作对比处理,可以先看下算法的定义:

		// TEMPLATE FUNCTION _Find_pr WITH PRED
template<class _InIt,
	class _Ty,
	class _Pr> inline
	_InIt _Find_pr(_InIt _First, _InIt _Last, const _Ty& _Val, _Pr _Pred)
	{	// find first matching _Val, using _Pred
	for (; _First != _Last; ++_First)
		if (_Pred(*_First, _Val))
			break;
	return (_First);
	}

算法的参数为容器的头迭代器,尾迭代器,传入值与仿函数,我们一般想做这种对比都会想到find()算法,它的实现方式为

template<class _InIt,
	class _Ty> inline
	_InIt _Find(_InIt _First, _InIt _Last, const _Ty& _Val, false_type)
	{	// find first matching _Val
	for (; _First != _Last; ++_First)
		if (*_First == _Val)
			break;
	return (_First);
	}

方法为遍历容器,当容器的值为指定值时,返回当前迭代器指针。

可见_Find_pr()顾名思义就是find函数的仿函数版本

下面做个演示

Bend = _Find_pr(booksRecord_.begin(), booksRecord_.end(), bc,
				[](Books* right, int BC) {
				if ((right->GetbooksCode()) == (BC)) {
					return 1;
				}
				return 0;
			});

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值