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
   );
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值