find_end 源码剖析

一:用法解析
函数原型:

equality (1)    
template <class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,
                              ForwardIterator2 first2, ForwardIterator2 last2);
predicate (2)   
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,
                              ForwardIterator2 first2, ForwardIterator2 last2,
                              BinaryPredicate pred);

功能:

在[ first1 , last1 )中搜索最后一个和区间[ first2 , last2 )匹配成功(默认是对应元素相等,重载是使pred为真)的区间,如果找到,返回[ first1 ,last1 )区间内最后匹配成功区间的首元素迭代器;否则返回last1。

例子:

// find_end example
#include <iostream>     // std::cout
#include <algorithm>    // std::find_end
#include <vector>       // std::vector

bool myfunction (int i, int j) {
  return (i==j);
}

int main () {
  int myints[] = {1,2,3,4,5,1,2,3,4,5};
  std::vector<int> haystack (myints,myints+10);

  int needle1[] = {1,2,3};

  // using default comparison:
  std::vector<int>::iterator it;
  it = std::find_end (haystack.begin(), haystack.end(), needle1, needle1+3);

  if (it!=haystack.end())
    std::cout << "needle1 last found at position " << (it-haystack.begin()) << '\n';

  int needle2[] = {4,5,1};

  // using predicate comparison:
  it = std::find_end (haystack.begin(), haystack.end(), needle2, needle2+3, myfunction);

  if (it!=haystack.end())
    std::cout << "needle2 last found at position " << (it-haystack.begin()) << '\n';

  return 0;
}
运行如下:

needle1 found at position 5
needle2 found at position 3


二:源码剖析
// TEMPLATE FUNCTION find_end WITH PRED
template<class _FwdIt1,
    class _FwdIt2,
    class _Diff1,
    class _Diff2,
    class _Pr> inline
    _FwdIt1 _Find_end(_FwdIt1 _First1, _FwdIt1 _Last1,
        _FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred, _Diff1 *, _Diff2 *)
    {   // find last [_First2, _Last2) satisfying _Pred
    _Diff1 _Count1 = 0;
    _Distance(_First1, _Last1, _Count1);//区间长度
    _Diff2 _Count2 = 0;
    _Distance(_First2, _Last2, _Count2);
    _FwdIt1 _Ans = _Last1;

    if (0 < _Count2)
        {   // validate _Pred and test
        _DEBUG_POINTER_IF(_Count2 <= _Count1, _Pred);//判断区间长度是否合理,函数指针是否有效
        for (; _Count2 <= _Count1; ++_First1, (void)--_Count1)
            {   // room for match, try it
            _FwdIt1 _Mid1 = _First1;
            for (_FwdIt2 _Mid2 = _First2; ; ++_Mid1)
                if (!_Pred(*_Mid1, *_Mid2))
                    break;
                else if (++_Mid2 == _Last2)
                    {   // potential answer, save it
                    _Ans = _First1;
                    break;
                    }
            }
        }
    return (_Ans);
    }

template<class _FwdIt1,
    class _FwdIt2,
    class _Pr> inline
    _FwdIt1 find_end(_FwdIt1 _First1, _FwdIt1 _Last1,
        _FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred)
    {   // find last [_First2, _Last2) satisfying _Pred
    _DEBUG_RANGE(_First1, _Last1);
    _DEBUG_RANGE(_First2, _Last2);
    return (_Rechecked(_First1,
        _Find_end(_Unchecked(_First1), _Unchecked(_Last1),
            _Unchecked(_First2), _Unchecked(_Last2), _Pred,
            _Dist_type(_First1), _Dist_type(_First2))));
    }

        // TEMPLATE FUNCTION find_end
template<class _FwdIt1,
    class _FwdIt2> inline
    _FwdIt1 find_end(_FwdIt1 _First1, _FwdIt1 _Last1,
        _FwdIt2 _First2, _FwdIt2 _Last2)
    {   // find last [_First2, _Last2) match
    return (_STD find_end(_First1, _Last1, _First2, _Last2,
        equal_to<>()));
    }

友情提醒,老套路,源码要从底端往上读。



源码摘抄自Visual Studio 2015安装目录algorithm文件中。



点击进入目录----> C++源码剖析目录



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值