STL算法之查找对象

beg,end:表示元素范围的迭代器。
unaryPred和binaryPred是一元和二元谓词,即分别接受一个和两个参数,都是来自输入序列的元素。
一:简单查找算法
1.find
find(beg,end,val)
函数原型为:
template<class _InIt,class _Ty> inline
    _InIt find(_InIt _First, _InIt _Last, const _Ty& _Val)
    {   // find first matching _Val
    _DEBUG_RANGE(_First, _Last);
    return (_Rechecked(_First,
        _Find(_Unchecked(_First), _Unchecked(_Last), _Val)));
    }
    find查找范围为[beg,end),返回值是一个迭代器,指向输入序列中第一个等于val的元素。如果在输入序列中未找到,则返回值为end;
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    vector<int> data = { 0,1,2,3,4,5,6,7 };
    vector<int>::iterator val_loc1 = find(data.begin(), data.end(), 5);
    vector<int>::iterator val_loc2 = find(data.begin(), data.end(), 8);
    if (val_loc1 != data.end())
        cout << "find the data in the vector:" << *val_loc1 << endl;
    else
        cout << "can not find the data in the vector" << endl;
    if (val_loc2 != data.end())
        cout << "find the data in the vector:" << *val_loc2 << endl;
    else
        cout << "can not find the data in the vector" << endl;
    system("pause");
    return 0;
}   
output:
find the data in the vector:5
can not find the data in the vector

2.find_if
find_if(beg,end,unaryPred)  在algorithm库中
函数原型为:
template<class _InIt,class _Pr> inline
    _InIt find_if(_InIt _First, _InIt _Last, _Pr _Pred)
    {   // find first satisfying _Pred
    _DEBUG_RANGE_PTR(_First, _Last, _Pred);
    return (_Rechecked(_First,
        _Find_if(_Unchecked(_First), _Unchecked(_Last), _Pred)));
    }
template<class _InIt, class _Pr> inline
    _InIt _Find_if(_InIt _First, _InIt _Last, _Pr _Pred)
    {   // find first satisfying _Pred
    for (; _First != _Last; ++_First)
        if (_Pred(*_First))
            break;
    return (_First);
    }
根据定义可知:指定的pred运算条件(以仿函数表示),循环查找[first,last)内的所有元素,找出第一个令pred运算结果true者。如果找到就返回一个迭代器指向该元素,否则就返回迭代器last。其中pred接受一个参数,返回值为bool形。
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
class Find_If {
public:
    bool operator() (int num)
    {
        return num == 8;
    }
};

bool find_if_fun(int num)
{
    return num == 5;
}
int main()
{
    vector<int> data = { 0,1,2,3,4,5,6,7 };
    vector<int>::iterator val_loc1 = find_if(data.begin(), data.end(), find_if_fun);
    vector<int>::iterator val_loc2 = find_if(data.begin(), data.end(), Find_If());
    if (val_loc1 != data.end())
        cout << "find the data in the vector:" << *val_loc1 << endl;
    else
        cout << "can not find the data in the vector" << endl;
    if (val_loc2 != data.end())
        cout << "find the data in the vector:" << *val_loc2 << endl;
    else
        cout << "can not find the data in the vector" << endl;
    system("pause");
    return 0;
}
output:
find the data in the vector:5
can not find the data in the vector

3.find_if_not
find_if_not(beg,end,unaryPred)
返回一个迭代器,指向输入序列中第一个令unaryPred为false的元素。如全为true,则返回end.
class Find_If {
public:
    bool operator() (int num)
    {
        return num == 1;
    }
};

bool find_if_fun(int num)
{
    return num == 0;
}
vector<int> data = { 0,0,0,0,0,0,0,0 };
    vector<int>::iterator val_loc1 = find_if_not(data.begin(), data.end(), find_if_fun);
    vector<int>::iterator val_loc2 = find_if_not(data.begin(), data.end(), Find_If());
can not find the data in the vector
find the data in the vector:0
二:查找重复值的算法
1.adjacent_find(beg,end)
2.adjacent_find(beg,end,binaryPred)
函数原型:
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_IF(_First != _Last && _STD next(_First) != _Last, _Pred);
    return (_Rechecked(_First,_Adjacent_find(_Unchecked(_First), _Unchecked(_Last), _Pred)));
    }
pred为空时,相邻两个元素相等,或者pred存在,相邻两个元素符合pred,则返回一个指向第一个元素的迭代器。不存在则返回为end.
bool adj(int num1,int num2)
{
    return num1 == num2*4;
}
int main()
{
    vector<int> data = { 1,2,3,3,4,1,0,0 };
    vector<int>::iterator val_loc1 = adjacent_find(data.begin(), data.end());
    vector<int>::iterator val_loc2 = adjacent_find(data.begin(), data.end(), adj);
    if (val_loc1 != data.end())
        cout << "find the data in the vector:" << *val_loc1 << endl;
    else
        cout << "can not find the data in the vector" << endl;
    if (val_loc2 != data.end())
        cout << "find the data in the vector:" << *val_loc2 << endl;
    else
        cout << "can not find the data in the vector" << endl;
    system("pause");
    return 0;
}
output:
find the data in the vector:3
find the data in the vector:4

3.search_n(beg,end,count,val)
4.search_n(beg,end,count,val,binaryPred)
返回一个迭代器,从此位置开始有count个相等或符合binaryPred的元素。不存在则返回end.
template<class ForwardIterator, class Size, class T> 
ForwardIterator search_n ( ForwardIterator first, ForwardIterator last, 
                           Size count, const T& value ) 
{ 
    ForwardIterator it, limit; 
    Size i; 

    limit=first; 
    advance(limit,distance(first,last)-count); 

    while (first!=limit) 
    { 
        it = first; 
        i=0; 
        while (*it==value)       // or: while (pred(*it,value)) for the pred version 
        { 
            ++it; 
            if (++i==count) return first; 
        } 
        ++first; 
    } 
    return last; 
} 
第二个参数是val;
bool sea(int num1,int num2)
{
    return num1 >num2;
}
int main()
{
    vector<int> data = { 1,2,4,8,4,1,0,0,0 };
    vector<int>::iterator val_loc1 = search_n(data.begin(), data.end(),4,0);
    vector<int>::iterator val_loc2 = search_n(data.begin(), data.end(), 2,2,sea);
    if (val_loc1 != data.end())
        cout << "find the data in the vector:" << *val_loc1 << endl;
    else
        cout << "can not find the data in the vector" << endl;
    if (val_loc2 != data.end())
        cout << "find the data in the vector:" << *val_loc2 << endl;
    else
        cout << "can not find the data in the vector" << endl;
    system("pause");
    return 0;
}
can not find the data in the vector
find the data in the vector:4

三:查找子序列的算法
1.search(beg1,end1,beg2,end2)
2.search(beg1,end1,beg2,end2,binaryPred)
返回第二个输入范围在第一个输入范围中第一次出现的位置,未找到则返回end1.
bool sea(int num1,int num2)
{
    return num1  ==num2*2;
}
int main()
{
    vector<int> data = { 0,2,4,8,4,1,0,0,0 };
    vector<int>data1 = { 2,4,};
    vector<int>::iterator val_loc1 = search(data.begin(), data.end(),data1.begin(),data1.end());
    vector<int>::iterator val_loc2 = search(data.begin(), data.end(), data1.begin(), data1.end(),sea);
    if (val_loc1 != data.end())
        cout << "find the data in the vector:" << *val_loc1 << endl;
    else
        cout << "can not find the data in the vector" << endl;
    if (val_loc2 != data.end())
        cout << "find the data in the vector:" << *val_loc2 << endl;
    else
        cout << "can not find the data in the vector" << endl;
    system("pause");
    return 0;
}
output:
find the data in the vector:2
find the data in the vector:4

3.find_first_of(beg1,end1,beg2,end2)
4.find_first_of(beg1,end1,beg2,end2,binaryPred)
返回一个迭代器,指向第二个输入范围任意元素在第一个元素中出现的位置。未找到则输出end1.
5.find_end(beg1,end1,beg2,end2)
6.find_end(beg1,end1,beg2,end2,binaryPred)
返回最后一次出现的位置,如果第二个输入范围为空,或者第一个输入范围未找到,则输出end1.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值