zoukankan      html  css  js  c++  java
  • STL中的查找算法

        STL中有很多算法,这些算法可以用到一个或多个STL容器(因为STL的一个设计思想是将算法和容器进行分离),也可以用到非容器序列比如数组中。众多算法中,查找算法是应用最为普遍的一类。

    单个元素查找

    1、 find() 比较条件为元素是否相等的查找:

        template <class InputIterator, class T>
        InputIterator find (InputIterator first, InputIterator last, const T& val);
    

    2、find_if() 自定义比较函数 
    从给出的区间中查找第一个满足比较函数的元素

    bool cmpFunction (int i) {
      return ((i%30)==0);
    }
    it = std::find_if (myvector.begin(), myvector.end(), cmpFunction);
    std::cout << "first:" <<  *it <<std::endl;
    

    3、count() 统计元素出现的次数 
    std::count() 统计区间中某个元素出现的次数 
    std::count_if() 自定义比较函数

    4、min_element() 查找给定区间内最小值

    5、max_element() 查找给定区间内最大值

    6、binary_search() 有序区间的二分查找 
    binary_search() 用来在一个有序区间中使用二分法查找元素是否在这个区间中,该算法的返回值为bool表示是否存在

    template <class ForwardIterator, class T>
      bool binary_search (ForwardIterator first, ForwardIterator last, const T& val)
    {
      first = std::lower_bound(first,last,val);
      return (first!=last && !(val<*first));
    }
    

    7、lower_bound() 返回有序序列给定区间[first, last) (左闭右开)内的第一个大于等于value的位置。如果所有元素都小于value,则返回last。

    template< class ForwardIterator, class Type >
    ForwardIterator lower_bound( ForwardIterator first, ForwardIterator last, const Type &value );
    
    template< class ForwardIterator, class Type, class Compare>
    ForwardIterator lower_bound( ForwardIterator first, ForwardIterator last, const Type &value, Compare comp ); //支持自定义比较函数
    

    8、upper_bound() 返回有序序列给定区间[first, last) (左闭右开)内的第一个大于value的位置。如果所有元素都小于等于value,则返回last。

    template< class ForwardIterator, class Type >
    ForwardIterator upper_bound( ForwardIterator first, ForwardIterator last, const Type &value );
    
    template< class ForwardIterator, class Type, class Compare>
    ForwardIterator upper_bound( ForwardIterator first, ForwardIterator last, const Type &value, Compare comp ); //支持自定义比较函数
    

    其中lower_bound/upper_bound 可以用于任何支持随机访问的容器中,比如vector,deque,数组。对于不支持随机访问的容器如 set/map,这些容器有同名的方法来进行 lower_bound/upper_bound 操作。

    map::lower_bound(key):返回map中第一个大于或等于key的迭代器指针
    map::upper_bound(key):返回map中第一个大于key的迭代器指针
    set::lower_bound(val):返回set中第一个大于或等于val的迭代器指针
    set::upper_bound(val):返回set中第一个大于val的迭代器指针
    

    区间查找(区间整体匹配)

    1、search() 查找子区间首次出现的位置 
    find() 用来查找单个元素,search() 用来查找一个子区间,比如 从myvector中查找自取件[20, 30] 的位置

    int needle1[] = {20,30};
    it = std::search (myvector.begin(), myvector.end(), needle1, needle1+2);
    if (it!=myvector.end())
    std::cout << "needle1 found at position " << (it-myvector.begin()) << '
    ';
    

    search() 支持自定义比较函数,比如查询给定区间中每个元素比目标区间小1的子区间:

    bool cmpFunction (int i, int j) {
      return (i-j==1);
    }
    int myints[] = {1,2,3,4,5,1,2,3,4,5};
    std::vector<int> haystack (myints,myints+10);
    
    int needle2[] = {1,2,3};
    // using predicate comparison:
    it = std::search (haystack.begin(), haystack.end(), needle2, needle2+3, cmpFunction);
    

    2、find_end() 查找子区间最后一次出现的位置 
    search()查找子区间第一次出现的位置,而find_end() 用来查找子区间最后一次出现的位置,find_end()支持自定义比较函数。

    3、equal() 判断两个区间是否相等

    4、mismatch() 查询两个区间首次出现不同的位置

    集合查找(集合内任意一个元素匹配)

    find_first_of() 查找给定集合中的任意一个元素

  • 相关阅读:
    IIS应用程序池标识(程序池账户)ApplicationPoolIdentify
    笔记二 sql 基础知识
    笔记三 索引讲解
    SqlParameter使用参数错误,提示请求超时或则查询速度过慢
    SVN 常见操作
    Excel 基本操作
    sql server row_number分页排序
    本地部署IIS
    sql中去掉字段的所有空格
    sql取逗号前后数据与批量修改某一字段某一值
  • 原文地址:https://www.cnblogs.com/gtarcoder/p/5811725.html
Copyright © 2011-2022 走看看