日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL--lower_bound()upper_bound();

發布時間:2024/4/14 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL--lower_bound()upper_bound(); 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

又是兩個黑科技一般的存在。

首先我們來介紹一下這兩個函數:

?

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的位置。

?

如圖:

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

兩種函數均采用了二分的方法。

?

注意:

函數調用序列必須有序。

upper_bound()和lower_bound()的返回值都是迭代器的位置,不能直接與int等類型進行賦值。

?

?

std::lower_bound:

default (1)
template <class ForwardIterator, class T>ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,const T& val);
custom (2)
template <class ForwardIterator, class T, class Compare>ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,const T& val, Compare comp);

作用:

返回一個非遞減序列[first, last)中的第一個大于等于值val的位置。

?

源代碼為:

1 template <class ForwardIterator, class T> 2 ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val) 3 { 4 ForwardIterator it; 5 iterator_traits<ForwardIterator>::difference_type count, step; 6 count = distance(first,last); 7 while (count>0) 8 { 9 it = first; step=count/2; advance (it,step); 10 if (*it<val) { // or: if (comp(*it,val)), for version (2) 11 first=++it; 12 count-=step+1; 13 } 14 else count=step; 15 } 16 return first; 17 }

?

Example:

1 // lower_bound/upper_bound example 2 #include <iostream> // std::cout 3 #include <algorithm> // std::lower_bound, std::upper_bound, std::sort 4 #include <vector> // std::vector 5 6 int main () { 7 int myints[] = {10,20,30,30,20,10,10,20}; 8 std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20 9 10 std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 11 12 std::vector<int>::iterator low,up; 13 low=std::lower_bound (v.begin(), v.end(), 20); // ^ 14 up= std::upper_bound (v.begin(), v.end(), 20); // ^ 15 16 std::cout << "lower_bound at position " << (low- v.begin()) << '\n'; 17 std::cout << "upper_bound at position " << (up - v.begin()) << '\n'; 18 19 return 0; 20 }

?

Output:

lower_bound at position 3 upper_bound at position 6

?

資料參考:

http://www.cplusplus.com/reference/algorithm/lower_bound/

?

std::upper_bound:

default (1)custom (2)
template <class ForwardIterator, class T>ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,const T& val);
template <class ForwardIterator, class T, class Compare>ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,const T& val, Compare comp);

作用:

返回一個非遞減序列[first, last)中第一個大于val的位置。

?

源代碼為:

1 template <class ForwardIterator, class T> 2 ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& val) 3 { 4 ForwardIterator it; 5 iterator_traits<ForwardIterator>::difference_type count, step; 6 count = std::distance(first,last); 7 while (count>0) 8 { 9 it = first; step=count/2; std::advance (it,step); 10 if (!(val<*it)) // or: if (!comp(val,*it)), for version (2) 11 { first=++it; count-=step+1; } 12 else count=step; 13 } 14 return first; 15 }

?

Example:

1 // lower_bound/upper_bound example 2 #include <iostream> // std::cout 3 #include <algorithm> // std::lower_bound, std::upper_bound, std::sort 4 #include <vector> // std::vector 5 6 int main () { 7 int myints[] = {10,20,30,30,20,10,10,20}; 8 std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20 9 10 std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 11 12 std::vector<int>::iterator low,up; 13 low=std::lower_bound (v.begin(), v.end(), 20); // ^ 14 up= std::upper_bound (v.begin(), v.end(), 20); // ^ 15 16 std::cout << "lower_bound at position " << (low- v.begin()) << '\n'; 17 std::cout << "upper_bound at position " << (up - v.begin()) << '\n'; 18 19 return 0; 20 }

?

Output:

lower_bound at position 3 upper_bound at position 6

?

資料參考:

http://www.cplusplus.com/reference/algorithm/upper_bound/

?

附一道簡單例題:

Interesting drink

轉載于:https://www.cnblogs.com/Kiven5197/p/5767583.html

總結

以上是生活随笔為你收集整理的STL--lower_bound()upper_bound();的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。