stl 的 二分函式

2022-08-30 04:24:13 字數 1002 閱讀 4704

在stl裡面有二分函式

lower_bound  和 upper_bound;

第一引數 為起始位址 第二個為結束位址 注意 它是左閉右開的(即不包括結束位址對應的那個值)

第三個引數為要找的值; 兩個二分函式都是要在由小到大的序列裡。

然後我們開始介紹下第四個引數

比較函式 (我也不太清楚是什麼)

只要你在第四個引數上面 打上 greater()(他的標頭檔案 #include)

lower_bound 就會 變成 返回陣列中第乙個小於或等於被查數的位址

upper_bound 就會 變成 返回陣列中第乙個小於被查數的位址

so interesting

附上求最長下降子串行的**

#include#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define m 5100

#define min(x , y) (x) < (y) ? (x) : (y)

#define max(x , y) (x) > (y) ? (x) : (y)

#define inf 0x3f3f3f3f

#define ll long long

using

namespace

std;

int n , f[m] , ans = -inf , a[m];

intmain()

f[0] =inf;

for (int i = 1 ; i <= n ; ++i)

for (int i = 1 ; f[i] ; ++i) ans =i;

printf ("%d

", ans);

return0;

}

stl的二分函式

1.stable sort在陣列裡和sort函式是一樣的,區別在於前者對於數值相等的兩個變數並不會交換順序,所以只有在結構體裡才能看出區別 2.lower bound函式返回陣列中第乙個大於等於某數的位置,而upper bound返回陣列中第乙個大於某數的位置。所以兩個函式相減經常用來求乙個陣列之內...

stl 二分查詢

在stl中對二分查詢進行了封裝,有兩種 upper bound,lower bound。例如 pos lower bound a,a n,value 查詢value值在長度為n的陣列a中的位置 關於返回值,函式lower bound 在first和last中的前閉後開區間進行二分查詢,返回大於或等於...

STL 二分查詢

實現原始碼 1.在乙個遞增的陣列 或vector 中查詢元素屬於 s e 的下標 2.查詢遞增陣列中元素是否存在 使用binary search 注 對於結構體,要麼過載小於符號 bool operator 要麼定義有小於符號含義的cmp函式。3.應用在遞減序列中 include include i...