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


     

  • 相关阅读:
    Git服务器配置及本地克隆提交、服务器获取
    远程编译获取控制台日志信息
    切换分支更改项目之一二事
    linux(乌班图)修改apt下载源
    表id关联数据获取至页面,制作下拉框多选进行数据多项获取(字段处理)
    实例实现测试用例自动生成
    maven一模块字段调用另一个模块数据生成下拉框
    页面搜索框日期搜索条件数据至后台的类型转换(时间戳)
    IDEA报错:Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. ('crmWatcherService'错误)
    单表(SSM、SpringBoot、SpringCloud、Freemaker、BootStrap等)
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7163099.html
Copyright © 2011-2022 走看看