zoukankan      html  css  js  c++  java
  • STL: 查找

    二分查找

    binary_search

    Tests whether there is an element in a sorted range that is equal to a specified value or that is equivalent to it in a sense specified by a binary predicate.

    template<class ForwardIterator, class Type>
       bool binary_search(
          ForwardIterator _First, 
          ForwardIterator _Last,
          const Type& _Val
       );
    template<class ForwardIterator, class Type, class BinaryPredicate>
       bool binary_search(
          ForwardIterator _First, 
          ForwardIterator _Last,
          const Type& _Val, 
          BinaryPredicate _Comp
       );

    lower_bound

    Finds the position of the first element in an ordered range that has a value greater than or equivalent to a specified value, where the ordering criterion may be specified by a binary predicate.

    template<class ForwardIterator, class Type>
       ForwardIterator lower_bound(
          ForwardIterator _First, 
          ForwardIterator _Last,
          const Type& _Val
       );
    template<class ForwardIterator, class Type, class BinaryPredicate>
       ForwardIterator lower_bound(
          ForwardIterator _First, 
          ForwardIterator _Last,
          const Type& _Val,
          BinaryPredicate _Comp
       );

    upper_bound

    Finds the position of the first element in an ordered range that has a value that is greater than a specified value, where the ordering criterion may be specified by a binary predicate.

    template<class ForwardIterator, class Type>
       ForwardIterator upper_bound(
          ForwardIterator _First, 
          ForwardIterator _Last,
          const Type& _Val
       );
    template<class ForwardIterator, class Type, class Predicate>
       ForwardIterator upper_bound(
          ForwardIterator _First, 
          ForwardIterator _Last,
          const Type& _Val,
          Predicate _Comp
       );

    equal_range

    Given an ordered range, finds the subrange in which all elements are equivalent to a given value.

    template<class ForwardIterator, class Type>
       pair<ForwardIterator, ForwardIterator> equal_range(
          ForwardIterator first,
          ForwardIterator last, 
          const Type& val
       );
    template<class ForwardIterator, class Type, class Predicate>
       pair<ForwardIterator, ForwardIterator> equal_range(
          ForwardIterator first,
          ForwardIterator last, 
          const Type& val, 
          Predicate comp
       );

    注:The first iterator of the pair returned by the algorithm is lower_bound, and the second iterator is upper_bound.

    A pair of forward iterators that specify a subrange, contained within the range searched, in which all of the elements are equivalent to val in the sense defined by the binary predicate used (either comp or the default, less-than).

    If no elements in the range are equivalent to val, the returned pair of forward iterators are equal and specify the point where val could be inserted without disturbing the order of the range.

     虽然二分查找的时间复杂度比顺序查找低,但是使用二分查找的前提为序列有序。所以对于查找次数较少,排序时间较长的序列执行查找,顺序查找是一个很好的选择,此外,顺序查找不会破坏原序列中元素的顺序。

    顺序查找

    find 

    Locates the position of the first occurrence of an element in a range that has a specified value.

    template<class InputIterator, class Type>
       InputIterator find(
          InputIterator _First, 
          InputIterator _Last, 
          const Type& _Val
       );

    find_if

    Locates the position of the first occurrence of an element in a range that satisfies a specified condition.

    template<class InputIterator, class Predicate>
       InputIterator find_if(
          InputIterator _First, 
          InputIterator _Last, 
          Predicate _Pred
       );

    find_if_not

    Returns the first element in the indicated range that does not satisfy a condition.

    template<class InputIterator, class Predicate>
        InputIterator find_if_not(
            InputIterator _First, 
            InputIterator _Last,
            BinaryPredicate _Comp
        );

    find_first_of

    Searches for the first occurrence of any of several values within a target range or for the first occurrence of any of several elements that are equivalent in a sense specified by a binary predicate to a specified set of the elements.

    template<class ForwardIterator1, class ForwardIterator2>
       ForwardIterator1 find_first_of(
          ForwardIterator1 _First1, 
          ForwardIterator1 _Last1,
          ForwardIterator2 _First2, 
          ForwardIterator2 _Last2
       );
    template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
       ForwardIterator1 find_first_of(
          ForwardIterator1 _First1, 
          ForwardIterator1 _Last1,
          ForwardIterator2 _First2, 
          ForwardIterator2 _Last2,
          BinaryPredicate _Comp
       );

    find_end

    Looks in a range for the last subsequence that is identical to a specified sequence or that is equivalent in a sense specified by a binary predicate.

    template<class ForwardIterator1, class ForwardIterator2>
       ForwardIterator1 find_end(
          ForwardIterator1 _First1, 
          ForwardIterator1 _Last1,
          ForwardIterator2 _First2, 
          ForwardIterator2 _Last2
       );
    template<class ForwardIterator1, class ForwardIterator2, class Pred>
       ForwardIterator1 find_end(
          ForwardIterator1 _First1, 
          ForwardIterator1 _Last1,
          ForwardIterator2 _First2, 
          ForwardIterator2 _Last2,
          Pred _Comp
       );

    search

    Searches for the first occurrence of a sequence within a target range whose elements are equal to those in a given sequence of elements or whose elements are equivalent in a sense specified by a binary predicate to the elements in the given sequence.

    template<class ForwardIterator1, class ForwardIterator2>
       ForwardIterator1 search(
          ForwardIterator1 _First1, 
          ForwardIterator1 _Last1,
          ForwardIterator2 _First2, 
          ForwardIterator2 _Last2
       );
    template<class ForwardIterator1, class ForwardIterator2, class Predicate>
       ForwardIterator1 search(
          ForwardIterator1 _First1, 
          ForwardIterator1 _Last1,
          ForwardIterator2 _First2, 
          ForwardIterator2 _Last2
          Predicate _Comp
       );

    注:search与find_end都是查找一个区域是否在源区域中出现。名字不一致!

    search_n

    Searches for the first subsequence in a range that of a specified number of elements having a particular value or a relation to that value as specified by a binary predicate.

    template<class ForwardIterator1, class Diff2, class Type>
       ForwardIterator1 search_n(
          ForwardIterator1 _First1, 
          ForwardIterator1 _Last1,
          Diff2 _Count, 
          const Type& _Val
       );
    template<class ForwardIterator1, class Diff2, class Type, class BinaryPredicate>
       ForwardIterator1 search_n(
          ForwardIterator1 _First1, 
          ForwardIterator1 _Last1,
          Diff2 _Count, 
          const Type& _Val,
          BinaryPredicate _Comp
       );
  • 相关阅读:
    案例19-页面使用ajax显示类别菜单
    案例18-首页最新商品和热门商品显示
    案例17-validate自定义校验规则校验验证码是否输入正确
    案例16-validate自定义校验规则校验用户名是否存在
    案例15-基本的表单校验使用validate
    测开之路六十九:监控平台之视图层
    测开之路六十八:监控平台之监控逻辑和处理逻辑
    测开之路六十七:监控平台之附加功能准备
    测开之路六十六:UI测试平台之处理逻辑和蓝图添加到程序入口
    测开之路六十五:UI测试平台之js
  • 原文地址:https://www.cnblogs.com/freewater/p/2948126.html
Copyright © 2011-2022 走看看