lower_bound(v.begin(), v.end(), x)
lower_bound(v.begin(), v.end(), x)
我是 C++ 菜鸟,我刚看到这段代码
x = v[lft]+k;
low = lower_bound(v.begin(), v.end(), x) - v.begin();
if(low >= n|| v[low] > x )
low--;
center = v[low];
如果向量中不存在 x,则它 returns 的值大于向量的大小。这证明搜索是否 low >= n
是合理的,但我无法弄清楚在什么情况下 v[low] > x
会满足这个条件?不应该 low
是 x 存在的索引,所以怎么会有大于 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.
详情请看这里:lower_bound - cppreference
我是 C++ 菜鸟,我刚看到这段代码
x = v[lft]+k;
low = lower_bound(v.begin(), v.end(), x) - v.begin();
if(low >= n|| v[low] > x )
low--;
center = v[low];
如果向量中不存在 x,则它 returns 的值大于向量的大小。这证明搜索是否 low >= n
是合理的,但我无法弄清楚在什么情况下 v[low] > x
会满足这个条件?不应该 low
是 x 存在的索引,所以怎么会有大于 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.
详情请看这里:lower_bound - cppreference