1. 程式人生 > >C++中lower_bound函式和upper_bound函式

C++中lower_bound函式和upper_bound函式

STL中關於二分查詢的函式有三個lower_bound 、upper_bound 、binary_search 。這三個函式都運用於有序區間(當然這也是運用二分查詢的前提),下面記錄一下這兩個函式。

ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)演算法返回一個非遞減序列[first, last)中的第一個大於等於值val的位置。

ForwardIter upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)演算法返回一個非遞減序列[first, last)中的第一個大於值val的位置。

lower_bound和upper_bound如下圖所示:

\