lower_bound的使用

unction template
<algorithm>

std::lower_bound

default (1)
template <class ForwardIterator, class T>
  ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
                               const T& val);
custom (2)
template <class ForwardIterator, class T, class Compare>
  ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
                               const T& val, Compare comp);
Return iterator to lower bound
Returns an iterator pointing to the first element in the range[first,last) which does not compare less than val.

The elements are compared using operator< for the first version, andcomp for the second. The elements in the range shall already be sorted according to this same criterion (operator< or comp), or at leastpartitioned with respect toval.

The function optimizes the number of comparisons performed by comparing non-consecutive elements of the sorted range, which is specially efficient forrandom-access iterators.

Unlike upper_bound, the value pointed by the iterator returned by this function may also be equivalent toval, and not only greater.


Parameters

first, last
Forward iterators to the initial and final positions of asorted (or properlypartitioned) sequence. The range used is[first,last), which contains all the elements between first andlast, including the element pointed by first but not the element pointed bylast.
val
Value of the lower bound to search for in the range.
For (1), T shall be a type supporting being compared with elements of the range[first,last) as the right-hand side operand of operator<.
comp
Binary function that accepts two arguments (the first of the type pointed byForwardIterator, and the second, always val), and returns a value convertible tobool. The value returned indicates whether the first argument is considered to go before the second.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.

Return value

An iterator to the lower bound of val in the range.
If all the element in the range compare less than val, the function returnslast.


Complexity

On average, logarithmic in the distance between first and last: Performs approximatelylog2(N)+1 element comparisons (where N is this distance).
On non-random-access iterators, the iteratoradvances produce themselves an additional linear complexity inN on average.



注解:第一个函数不需要自定义比较运算符,第二个需要。

lower_bound返回第一个满足大于等于指定val值的元素的迭代器,upper_bound返回最后一个满足大于等于指定val值的元素的迭代器。找不到满足条件的元素就返回end迭代器。

查找的时间复杂度是log2(N)+1


实例程序:

// lower_bound/upper_bound example
#include <iostream>     // std::cout
#include <algorithm>    // std::lower_bound, std::upper_bound, std::sort
#include <vector>       // std::vector

int main () {
  int myints[] = {10,20,30,30,20,10,10,20};
  std::vector<int> v(myints,myints+8);           // 10 20 30 30 20 10 10 20

  std::sort (v.begin(), v.end());                // 10 10 10 20 20 20 30 30

  std::vector<int>::iterator low,up;
  low=std::lower_bound (v.begin(), v.end(), 20); //          ^
  up= std::upper_bound (v.begin(), v.end(), 20); //                   ^

  std::cout << "lower_bound at position " << (low- v.begin()) << '\n';
  std::cout << "upper_bound at position " << (up - v.begin()) << '\n';

  return 0;
}

运行结果:

lower_bound at position 3
upper_bound at position 6






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值