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!
    ******/


     

  • 相关阅读:
    clientHeight、offsetHeight、scrollHeight详解
    JavaScript中常见的字符串操作函数及用法
    获取伪元素的属性和改变伪元素的属性
    HTML和CSS实现左侧固定宽度右侧内容可滚动
    gulp常用插件
    gulp入门详细教程
    Javascript 异步实现机制
    JavaScript:彻底理解同步、异步和事件循环(Event Loop)
    chrome调试,打完断点后关于JS的几个控制介绍
    js断点调试心得
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7163099.html
Copyright © 2011-2022 走看看