日韩性视频-久久久蜜桃-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();的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 国产调教打屁股xxxx网站 | 一区二区三区四区视频 | 色亚洲成人| 亚洲伊人网站 | 日本在线免费视频 | 91九色蝌蚪视频 | 天天综合天天添夜夜添狠狠添 | 影音先锋久久久久av综合网成人 | 久久久久久免费视频 | 91在线观看成人 | 碧蓝之海动漫在线观看免费高清 | 777精品久无码人妻蜜桃 | 1024手机看片日韩 | 久久久久无码国产精品一区 | 婷婷黄色网| 日韩在线第一区 | 在线能看的av | 啪啪资源| 欧美中文字幕在线观看 | japanese国产打屁股网站 | 美女av网址 | 男女啪啪免费 | 国产suv精品一区二区60 | 国产精品免费入口 | 日产久久久久久 | 在线你懂的视频 | 爆操女秘书 | 三级在线看中文字幕完整版 | 四虎黄色片 | 国内一级黄色片 | 国产成人一区二区三区别 | 最新色站 | 中文字幕一区二区三区av | 男女网站在线观看 | h视频在线看| 夜夜夜夜爽| 911精品国产一区二区在线 | 欧美日韩亚洲激情 | av卡一卡二| 日本三级中国三级99人妇网站 | 日本精品一区二区视频 | 影音先锋 日韩 | 亚洲欧美一区二区三区在线观看 | 网站一级片 | 91丨九色丨丰满人妖 | 6—12呦国产精品 | 日韩精品久久久久久免费 | 国产精品aaaa| 五月天av网站 | www.555国产精品免费 | 国产精品一二三区 | 久草影视在线 | 国产看片网站 | 欧美成人福利视频 | 日韩毛片一区二区三区 | 久久成人精品视频 | 日本h在线观看 | 天天躁日日躁狠狠躁av麻豆 | 在线观看国产精品视频 | 黄色片免费的 | h片网站在线观看 | 大白屁股一区二区视频 | 2021天天干 | 在线观看a网站 | 久久精品毛片 | 日本一区视频在线播放 | 色噜噜狠狠一区二区 | 国产欧美一区二区在线 | 91国偷自产一区二区三区女王 | 中文字幕一区二区三区四区五区 | 先锋影音在线 | 国产精品久久久爽爽爽麻豆色哟哟 | 兔费看少妇性l交大片免费 日韩高清不卡 | 久久综合99 | 欧美激情久久久久 | 天啪 | 欧美动态视频 | 91你懂的| 日韩欧美片 | 色多多在线视频 | av资源新版在线天堂 | 在线一区二区视频 | 丁香av| 污片视频在线观看 | 国产在线拍揄自揄拍无码 | 少妇人妻丰满做爰xxx | 日韩xxx高潮hd | 国产原创视频在线观看 | 先锋资源在线视频 | 成人毛片网站 | 小黄网站在线观看 | 最近最好的2019中文 | 精彩视频一区二区三区 | 成人网av| 国产一区欧美二区 | 性――交――性――乱a | 少妇性bbb搡bbb爽爽爽欧美 | 久久久91精品国产一区二区三区 | 一区二区在线观看免费 |