adjacen_find详解

来源:互联网 发布:淘宝性保健品吗 编辑:程序博客网 时间:2024/06/03 18:03

adjacen_find:搜索给定区间中两个连续相等的元素

这个函数也以重载的形式给出了这两个函数.一个是默认的关系运算(==),一个是仿函数

//TEMPLATE FUNCTION adjacent_find

template<class_FwdIt>inline

         _FwdIt _Adjacent_find(_FwdIt _First,_FwdIt _Last)

         {       // find first matching successor

         if(_First != _Last)

                   for(_FwdIt _Firstb; (_Firstb = _First), ++_First != _Last; )//首先保存起始元素.然后和下一个                                                                                                         //元素进行比较.如果相等则返回之前保

                                                                                                                //存的那个元素,否则进行下次比较.

         //需要注意的是,这里用了一些技巧,首先是使用了逗号表达式,其次是使用++_First == _Last.

                            if (*_Firstb == *_First)

                                     return (_Firstb);

         return(_Last);

         }

 

template<class_FwdIt>inline

         _FwdIt adjacent_find(_FwdIt _First,_FwdIt _Last)

         {       // find first matching successor

         _DEBUG_RANGE(_First, _Last);

         return(_Rechecked(_First,

                   _Adjacent_find(_Unchecked(_First),_Unchecked(_Last))));

         }

adjacen_find:搜索给定区间中连续使得_Pred为真的元素位置.

//TEMPLATE FUNCTION adjacent_find WITH PRED

template<class _FwdIt,

         class_Pr> inline

         _FwdIt _Adjacent_find(_FwdIt _First,_FwdIt _Last, _Pr _Pred)

         {       // find first satisfying _Pred with successor

         if(_First != _Last)

                   for(_FwdIt _Firstb; (_Firstb = _First), ++_First != _Last; )

                            if (_Pred(*_Firstb, *_First))

                                     return (_Firstb);

         return(_Last);

         }

 

template<class _FwdIt,

         class_Pr> inline

         _FwdIt adjacent_find(_FwdIt _First,_FwdIt _Last, _Pr _Pred)

         {       // find first satisfying _Pred with successor

         _DEBUG_RANGE(_First, _Last);

         _DEBUG_POINTER(_Pred);

         return(_Rechecked(_First,

                   _Adjacent_find(_Unchecked(_First),_Unchecked(_Last), _Pred)));

         }

照样还是举例说明吧!

举例:

template<typenameT>

bool equal_three( T _value1,T _value2 )

{

         return_value1 == ++ _value2;

}

int main()

{

         vector<int>vecInt;

         vecInt.push_back( 5 );

         vecInt.push_back( 2 );

         vecInt.push_back( 3 );

         vecInt.push_back( 2 );

         vecInt.push_back( 9 );

         vecInt.push_back( 9 );

         vecInt.push_back( 5 );

         vecInt.push_back( 4 );

         vecInt.push_back( 3 );

 

         vector<int>::iteratoriterFind = adjacent_find( vecInt.begin(),vecInt.end() );

         if (iterFind != vecInt.end() )

         {

                   cout<<*(--iterFind)<<"\n";

         }

         iterFind = adjacent_find(vecInt.begin(),vecInt.end(),equal_three<int>);

         if (iterFind != vecInt.end() )

         {

                   cout<<distance(vecInt.begin(),iterFind ) + 1<<"\n";

         }

         system( "pause");

         return0;

}

程序输出:

2

3

需要注意的是前一个2是vecInt的第3个元素.仿函数那个例子需要注意是*_First== ++ *_First来比较

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 阿里巴巴一键代销被投诉受假怎么办 登录小米云服务怎么删除密码怎么办 苹果6云空间连接不上怎么办 买家收到淘宝网交易异常通知怎么办 快递把我秒杀的货弄丢了怎么办 手机淘宝退货物流单号填错了怎么办 买家要求退货退款但是不发货怎么办 多给买家寄包裹不接电话怎么办 给买家发货物流单号错了怎么办 淘宝退货退款快递单号填错了怎么办 淘宝不小心点了延迟收货怎么办 淘宝快递地址错了货已经发了怎么办 卖家送运费险买家填错单号怎么办 买家无赖点了延迟收货卖家怎么办 淘宝卖家快递单号填错了怎么办 淘宝店有当天的快递忘记发货怎么办 在美国官网买东西少寄了东西怎么办 闲鱼退货货收了不退钱怎么办 闲鱼退货卖家收到东西不退钱怎么办 淘宝店铺物流服务给差评怎么办 在合肥东方融资网被骗了怎么办 微信今年绑定频繁明年再试怎么办 手机号码注销了微信密码忘了怎么办 摄像师把婚礼资料弄丢了怎么办 青岛拍的婚纱照修的不好怎么办 鞋店剩的鞋都是小码的怎么办 淘宝想买的衣服下架了怎么办 买家要求到付仲裁发生运费怎么办 淘宝退货退款页面刷新不出来怎么办 淘宝退货退款快递单号写错了怎么办 淘宝上已经退款的店家还发货怎么办 千牛买家下单付款了卖家怎么办 京东货到付款支付宝支付退款怎么办 美萍餐饮管理系统下单错误怎么办 淘宝店上传宝贝显示空间不足怎么办 淘宝店品牌被投诉未授权怎么办 淘宝天猫退货单号填错了怎么办 淘宝退货我把快递单号弄丢了怎么办 唯品会退货快递单号填错了怎么办 天猫换货写错运单号怎么办 训练衣舍的店铺名连接怎么办