function for finding last item less-than-or-equal to, like lower_bound

前端 未结 5 1748
南方客
南方客 2021-01-30 12:52

Is there a function in that uses binary search, like lower_bound but that returns the last item less-than-or-equal-to according to a given predic

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-30 13:35

    In a sorted container, the last element that is less than or equivalent to x, is the element before the first element that is greater than x.

    Thus you can call std::upper_bound, and decrement the returned iterator once. (Before decrementing, you must of course check that it is not the begin iterator; if it is, then there are no elements that are less than or equivalent to x.)

提交回复
热议问题