STL演算法中upper bound

2021-09-25 01:27:06 字數 1273 閱讀 5022

upper_bound()在前閉後開區間查詢的關鍵字的上界,返回大於val的第乙個元素位置。

首先介紹的是第一種用法:查詢比某個元素大的第乙個的值。

**如下:

vectora = ;

vector::iterator ite;

for_each(a.begin(), a.end(), (int a) );

cout << endl;

sort(a.begin(), a.end());

for_each(a.begin(), a.end(), (int a) );

cout << endl;

ite = upper_bound(a.begin(), a.end(), 2);    //返回陣列中第乙個大於被查數的值

if (ite != a.end())//返回值找到返回該數字位址,沒找到返回end

效果圖:

說明:排序之後,獲取第乙個值比2大的數,和它的索引值。

第二種用法,用到我們的反函式

**如下:

vectora = ;

vector::iterator ite;

for_each(a.begin(), a.end(), (int a) );

cout << endl;

sort(a.begin(), a.end());

for_each(a.begin(), a.end(), (int a) );

cout << endl;

ite = upper_bound(a.begin(), a.end(), 2);    //返回陣列中第乙個大於被查數的值

if (ite != a.end())//返回值找到返回該數字位址,沒找到返回end

說明:排序之後,獲取第乙個值比2小的數,和它的索引值。

STL中heap演算法(堆演算法)

push heap演算法 以下是push heap演算法的實現細節。該函式接收兩個迭代器,用來表現乙個heap底部容器 vector 的頭尾,而且新元素已經插入究竟部的最尾端。template inline void push heap randomaccessiterator first,rand...

STL中的equal演算法

bool equal inputit1 first1,inputit1 last1,inputit2 first2 first1,last1 the first range of the elements to compare first2 the beginning element of the ...

STL中的查詢演算法

stl中有很多演算法,這些演算法可以用到乙個或多個stl容器 因為stl的乙個設計思想是將演算法和容器進行分離 也可以用到非容器序列比如陣列中。眾多演算法中,查詢演算法是應用最為普遍的一類。單個元素查詢 1 find 比較條件為元素是否相等的查詢 template inputiterator fin...