Arjun Hegde Arjun Hegde - 4 months ago 31
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 )
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
be an index in which x is present, so how can there be a value greater than x?


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