zoukankan      html  css  js  c++  java
  • 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!
    ******/


     

  • 相关阅读:
    个人作业-Alpha项目测试
    第三次作业
    第二次作业
    第一次作业
    JQuery(一)页面加载,写入文本,对象转换,隐藏显示,基本选择器,层级选择器,基本过滤选择器,表单选择器,class操作,属性操作
    JavaScript(二)
    轮辐广告、简单选项卡
    div层随着页面大小变化相对位置不变、按钮隐藏一半鼠标放上去就出来,不放上去就退回去
    markDown语法详解
    Mybatis中动态SQL语句
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7163099.html
Copyright © 2011-2022 走看看