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
   );
<think>好的,用户想知道C++ STL中set的查找方法和实现。首先,我需要回忆一下set的基本特性。set是一个关联容器,存储唯一元素,并且元素是按顺序排列的。查找操作在set中很关键,因为它的高效性是其优势之一。 用户可能已经知道set使用红黑树实现,这是一种自平衡的二叉搜索树,所以查找的时间复杂度是O(log n)。但需要确认这一点是否正确。根据引用[4]提到的find函数,返回的是迭代器,如果找不到则返回end(),这和红黑树的查找方式一致。另外,引用[2]和[4]都提到了使用find成员函数进行查找,应该详细说明这个方法的使用方式。 接下来,用户可能想知道如何统计元素,虽然set中的元素是唯一的,但count方法在这里的作用可能只是确认存在性,因为count返回0或1。需要指出这一点,避免用户误解count的用途。 还有,用户可能对底层实现感兴趣,比如红黑树的结构如何支持快速查找。需要解释红黑树的基本性质,如每个节点的颜色、路径长度限制等,这些保证了树的平衡,从而使得查找效率高。 另外,引用[3]提到了通过重载运算符或自定义比较函数来改变排序规则,这可能会影响查找过程。需要说明自定义比较时需要注意的地方,比如必须保证与排序规则一致,否则查找可能出错。 用户的问题可能还涉及到性能分析,比如为什么set的查找比无序容器如unordered_set慢,但比线性结构快。这时候需要比较set和unordered_set的不同,前者是有序的,后者基于哈希表,查找时间复杂度为O(1),但哈希表有冲突的可能,实际性能取决于哈希函数的好坏。 可能还需要提到set的迭代器是双向的,支持前向和后向遍历,这在查找后遍历相邻元素时有用。例如,找到某个元素后,可以访问其前驱或后继节点。 此外,引用[1]中提到了unordered_set的遍历方式,虽然和set不同,但用户的问题集中在set上,所以应该专注于set的相关方法,但可以适当对比两者的查找方式,帮助用户更好理解。 总结一下,回答需要涵盖以下几点: 1. set的find成员函数的使用方法,返回迭代器或end()。 2. count方法的作用,返回0或1。 3. 底层红黑树的实现原理,以及如何保证查找效率。 4. 自定义比较函数对查找的影响。 5. 与unordered_set在查找上的对比。 6. 可能的错误使用场景,比如未排序的键导致查找失败。 需要确保每个部分都结合引用中的内容,比如引用[2]和[4]中的find和count示例,引用[3]的自定义比较函数,以及引用[1]中提到的无序容器的不同。同时,保持回答结构清晰,分点说明,并生成相关问题帮助用户进一步学习。</think>C++ STL set 查找元素的实现方法可分为以下三个层次分析: 一、基础API使用 1. `find()`成员函数:通过键值查找元素,返回迭代器 ```cpp set<int> s{1,3,5,7,9}; auto it = s.find(5); if(it != s.end()) cout << "Found: " << *it << endl; // 输出Found:5 else cout << "Not found" << endl; ``` 该方法时间复杂度为$O(\log n)$,底层采用红黑树实现[^4] 2. `count()`统计方法:返回元素存在数量(0或1) ```cpp if(s.count(3) > 0) cout << "Element exists"; ``` 二、底层实现原理 1. 红黑树特性: - 每个节点有红/黑颜色标记 - 根节点和叶子节点(NIL)为黑色 - 红色节点的子节点必为黑色 - 任意节点到叶子节点的路径包含相同数量黑节点 2. 查找流程: ```mermaid graph TD Start[开始] --> Compare{与当前节点比较} Compare --> |小于| Left[左子树] Compare --> |等于| Found[找到目标] Compare --> |大于| Right[右子树] Left --> Compare Right --> Compare Found --> End[返回迭代器] NIL节点 --> NotFound[返回end()] ``` 三、扩展应用技巧 1. 自定义比较规则时需保证与排序规则一致[^3] 2. 范围查找结合`lower_bound()`/`upper_bound()` ```cpp auto low = s.lower_bound(3); // 第一个>=3的元素 auto high = s.upper_bound(7); // 第一个>7的元素 s.erase(low, high); // 删除[3,7]区间 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值