Arjun Hegde Arjun Hegde - 20 days ago 10
C++ Question

lower_bound(v.begin(), v.end(), x)

I am a noob to c++, I just came across this code

x = v[lft]+k;
low = lower_bound(v.begin(), v.end(), x) - v.begin();

if(low >= n|| v[low] > x )
low--;
center = v[low];


if x is not present in the vector, it returns a value greater than the size of the vector. That justifies searching whether
low >= n
, but I cant figure out under which circumstance will this condition
v[low] > x
satisy?Should'nt
low
be an index in which x is present, so how can there be a value greater than x?

Answer

lower_bound returns

an iterator pointing to the first element in the range [first, last) that is not less than (i.e. greater or equal to) value.

See here for more detail: lower_bound - cppreference

Comments