相关疑难解决方法(0)

C lower_bound的实现

基于此处的以下定义

返回指向排序范围[first,last]中第一个元素的迭代器,它不会比value更小.使用operator <为第一个版本或comp为第二个版本进行比较.

什么是lower_bound()的C等效实现.我知道它将是二进制搜索的修改,但似乎无法确切地指出精确的实现.

int lower_bound(int a[], int lowIndex, int upperIndex, int e);
Run Code Online (Sandbox Code Playgroud)

示例案例:

int a[]= {2,2, 2, 7 };

lower_bound(a, 0, 1,2) would return 0 --> upperIndex is one beyond the last inclusive index as is the case with C++ signature.

lower_bound(a, 0, 2,1) would return 0.

lower_bound(a, 0, 3,6) would return 3;
lower_bound(a, 0, 4,6) would return 3; 
Run Code Online (Sandbox Code Playgroud)

我的尝试代码如下:

int low_bound(int low, int high, int e)
{
    if ( low < 0) return 0;
    if (low>=high …
Run Code Online (Sandbox Code Playgroud)

c algorithm binary-search lower-bound

21
推荐指数
2
解决办法
2万
查看次数

标签 统计

algorithm ×1

binary-search ×1

c ×1

lower-bound ×1