ここにある次の定義に基づいています
値未満を比較しない、ソートされた範囲[first、last)の最初の要素を指すイテレーターを返します。比較は、最初のバージョンの場合はoperator <、2番目のバージョンの場合はcompのいずれかを使用して行われます。
lower_bound()のCと同等の実装は何でしょうか。二分探索の修正になることは理解していますが、正確な実装を正確に特定することはできないようです。
int lower_bound(int a[], int lowIndex, int upperIndex, int e);
サンプルケース:
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;
私が試したコードを以下に示します。
int low_bound(int low, int high, int e)
{
if ( low < 0) return 0;
if (low>=high )
{
if ( e <= a[low] ) return low;
return low+1;
}
int mid=(low+high)/2;
if ( e> a[mid])
return low_bound(mid+1,high,e);
return low_bound(low,mid,e);
}