Arjun Hegde Arjun Hegde - 1 year ago 85
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?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download