find和find_if算法

1、STL算法里的find和find_if算法的使用:

区别于联系:联系:这个两个函数都是查找某个元素位置的方式;区别:其中find值适用于查找其里面元素值的位置。而find_if值使用与函数第三个参数的为判别式时的使用。

template<class InputIterator, class T>
InputIterator find ( InputIterator first, InputIterator last, const T& value )
{
for ( ;first!=last; first++) if ( *first==value ) break;
return first;
}

返回区间[first,end)中第一个值等于value的元素的位置。

如果没有找到匹配元素,则返回end。

复杂度:线性复杂度。最多比较次数是:元素的总个数。

程序实例:

下面的程序在int类型的vector中搜寻元素5和12,如果搜索到,就返回其位置if欧泽输出提示信息。

main.cpp(头文件algostuff.h和上一篇博客中的相同):
 

#include "algostuff.h"
using namespace std;
int main()
{
vector<int> intVec;
INSERT_ELEMENTS(intVec,1,9);
vector<int>::iterator pos;
pos = find(intVec.begin(),intVec.end(),5);
if(pos != intVec.end())
cout << "The value 5 exists,and its position is " <<
distance(intVec.begin(),pos) + 1 << endl;
else
cout << "The value 4 not found!" << endl;
pos = find(intVec.begin(),intVec.end(),12);
if(pos != intVec.end())
cout << "The value 12 exists,and its position is " <<
distance(intVec.begin(),pos) + 1 << endl;
else
cout << "The value 12 not found!" << endl;
}

 

二、find_if()算法

template<class InputIterator, class Predicate>
InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )
{
for ( ; first!=last ; first++ ) if ( pred(*first) ) break;
return first;
}

它在区间[first,end)中搜寻使一元判断式pred为true的第一个元素。

如果没找到,返回end。

程序实例:

下面程序找出第一个能够被3整除的元素,如果找到返回其位置。

main.cpp:

#include "algostuff.h"
using namespace std;
int main()
{
vector<int> intVec;
INSERT_ELEMENTS(intVec,1,9);
vector<int>::iterator pos;
pos = find_if(intVec.begin(),intVec.end(),
not1(bind2nd(modulus<int>(),3)));
if(pos != intVec.end())
cout << "The value divided by 3 exists,and the first value's position is " <<
distance(intVec.begin(),pos) + 1 << endl;
else
cout << "The value divided by 3 not found!" << endl;
}

 

还有一个使用例子就是:

    

                    // add old tracked objects
                        for (auto &i : old_result_vec) {
                            auto it = std::find_if(result_vec.begin(), result_vec.end(),
                                [&i](bbox_t const& b) { return b.track_id == i.track_id && b.obj_id == i.obj_id; });
                            //如果是等于迭代器的end(),则表明没有找到。
                            bool track_id_absent = (it == result_vec.end());
                            if (track_id_absent) {
                                //这里的-->就是大于或等于的比较符,这里是旧的帧目标也加到检测结果里
                                if (i.frames_counter-- > 1)
                                    result_vec.push_back(i);
                            }
                            else {
                                //每个box都有存储被跟踪帧计数,最多是3帧
                                it->frames_counter = std::min((unsigned)3, i.frames_counter + 1);
                            }
                        }

 

原文:https://blog.csdn.net/yangdashi888/article/details/80986075

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值