STL_算法_查找算法(find、find_if)

C++ Primer 学习中。。。

 

简单记录下我的学习过程 (代码为主)

 

find 、 find_if

 

 

/**********************线性查找O(n)
find();
find_if();
注意:
    1.如果是已序区间,可以使用区间查找算法
    2.关联式容器(set,map)有等效的成员函数find();时间复杂度O(log(n))
    3.string 有等效的成员函数find();
**********************/
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<set>
#include<algorithm>
#include<functional>
using namespace std;

/*************************************************************************************
std::find                                                                    algorithm
--------------------------------------------------------------------------------------
template <class InputIterator, class T>
   InputIterator find ( InputIterator first, InputIterator last, const T& value );

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

/*************************************************************************************
std::find_if                                                                    algorithm
--------------------------------------------------------------------------------------
template <class InputIterator, class Predicate>
   InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred );
eg:
template<class InputIterator, class Predicate>
  InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )
  {
    for ( ; first!=last ; first++ ) if ( pred(*first) ) break;
    return first;
  }
**************************************************************************************/
bool IsEven (int i);
int main ()
{
    int myints[] = {10,30,20,40,20,10,30,40};
    int * p;

    // pointer to array element:
    p = find(myints,myints+8,30);
    ++p;
    cout << "The element following 30 is " << *p << endl;

    vector<int> myvector (myints,myints+8);
    vector<int>::iterator it;

    // iterator to vector element:
    it = find (myvector.begin(), myvector.end(), 30);
    ++it;
    cout << "The element following 30 is " << *it << endl;

    //输出第一个30---第二个30区间内的数
    vector<int>::iterator it2;
    it2=find (it,myvector.end(),30);
    while(it!=it2)
        cout<<*it++<<" ";
    cout<<endl;

/**--------------------------------find_if()---------------------------------**/
//找第一个偶数
    it = find_if (myvector.begin(), myvector.end(), IsEven);//函数 或 函数对象
    cout << "The first odd value is " << *it << endl;

    it2 = find_if(myvector.begin(),myvector.end(), not1(bind2nd(modulus<int>(),2)));
    cout << "The first odd value is " << *it2 << endl;
/**--------------------------------关联容器---------------------------------**/
    set<int> s(myvector.begin(),myvector.begin()+4);
    cout<<"复杂度为O(log(n)),查找*s.find(40):= " << *s.find(40) << endl;
/**---------------------------------string----------------------------------**/
    string st("AngelaBaby");
    string::size_type pos = st.find("Baby");
    if(pos != string::npos)
        cout<<"find it!"<<endl;
    else cout<<"not find it!"<<endl;

    pos = st.find("baby");
    if(pos != string::npos)
        cout<<"find it!"<<endl;
    else cout<<"not find it!"<<endl;
    return 0;
}

bool IsEven (int i)
{
  return ((i%2)==0);
}

/******
Output:
    The element following 30 is 20
    The element following 30 is 20
    20 40 20 10
    The first odd value is 10
    The first odd value is 10
    复杂度为O(log(n)),查找*s.find(40):= 40
    find it!
    not find it!
******/

 

    原文作者:查找算法
    原文地址: https://blog.csdn.net/u010579068/article/details/50396620
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞