STL: lower_bound(·) and upper_bound(·)
2016-12-11 02:59
288 查看
Lower Bound
This is a problem I came across in Google interview, and I was caught off guard. The interviewer asked me to implement the lower_bound(·) function in STL. Here is the code:int lower_bound(int *array, int size, int key) { int first = 0, middle; int half, len; len = size; while(len > 0) { half = len >> 1; middle = first + half; if(array[middle] < key) { first = middle + 1; len = len-half-1; } else len = half; } return first; }
Upper Bound
Similarly we have:int upper_bound(int *array, int size, int key) { int first = 0, len = size-1; int half, middle; while(len > 0){ half = len >> 1; middle = first + half; if(array[middle] > key) len = half; else{ first = middle + 1; len = len - half - 1; } } return first; }
相关文章推荐
- STL 源码分析《5》---- lower_bound and upper_bound 详解
- C++ STL 之 lower_bound and upper_bound
- STL 源码分析《5》---- lower_bound and upper_bound 详解
- C++ STL 之 lower_bound and upper_bound
- C++ STL 之 lower_bound and upper_bound
- STL中lower_bound()、 upper_bound()的返回值
- STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())
- C/C++-STL中lower_bound与upper_bound的用法以及cmp函数
- STL之lower_bound和upper_bound
- STL源码学习----lower_bound和upper_bound算法
- 【STL源码学习】----lower_bound和upper_bound算法
- STL源码学习----lower_bound和upper_bound算法
- STL源码学习----lower_bound和upper_bound算法
- STL之lower_bound和upper_bound
- STL中的multimap---顺便说说如何查找同一关键字对应的所有值(利用count, lower_bound/upper_bound, equal_range)
- STL 中 lower_bound 与 upper_bound 与 二分查找
- STL upper_bound(),lower_bound()函数的学习+自己的实现
- STL 二分查找 upper_bound和lower_bound用法
- STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())
- 【STL】STL之lower_bound与upper_bound