upper怎么用,upper_bound 和lower_bound徹底搞懂

 2023-11-12 阅读 17 评论 0

摘要:1.? 問題引出 ? ? 今天在查看ORB_SLAM2注釋版源碼keyframe.cpp文件的時候,發現注釋者的意見: // http://www.cplusplus.com/reference/algorithm/upper_bound/ // 從mvOrderedWeights找出第一個大于w的那個迭代器 // 這里應該使用lower_bound,因為lower_

1.? 問題引出

? ? 今天在查看ORB_SLAM2注釋版源碼keyframe.cpp文件的時候,發現注釋者的意見:


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

// 從mvOrderedWeights找出第一個大于w的那個迭代器

// 這里應該使用lower_bound,因為lower_bound是返回小于等于,而upper_bound只能返回第一個大于的


仔細對比發現并沒有錯,估計注釋者并沒有深刻理解這個上闕界和下闕界。對lower_bound和upper_bound翻譯為這個數學術語是有原因的,同于數學中常用的范圍域“ [? ) ”。

2. 徹底明白lower_bound和upper_bound

upper怎么用。????小伙伴們可能從很多博客看到類似上述注釋者這樣的指導說明,但是往往讓人難以理解,有點擾亂思路、抓狂。很簡單的理解方法,數學中“a\in?[? ) ”這串數學符號表示下界“[”小于等于a,上界“)”大于a,在數學中叫做“半開半閉”區間,即左閉右開。

? ? 說列這么多,那如何和c++中的vector list等聯系起來,把vector/llist的數據看成一條數軸,給定比較值的左邊是lower_bound---"[",右邊是upper_bound---")",這兩個函數的使用要求vector/list等是排序好的,那是升序還是降序來著?默認情況是升序,降序需要指定。引入數學的理解,相信小伙伴們不會懵逼了,如果會,我也沒辦法。行上個代碼增加一下理解。

3. 代碼Code解釋

3.1 升序情況

? 代碼來了:

#include<iostream>

#include<algorithm>

#include<vector>

upperbound?#include<iterator>

using namespace std;

static bool weightComp( int a, int b)

????{

? ? ? ? return a>b;

? ? }

upperbound用法,int main(int argc,char**argv)

{

? ? std::vector<int> vecInt;

? ? vecInt.push_back(1);

? ? vecInt.push_back(3);

? ? vecInt.push_back(19);

upper and lower bounds,? ? vecInt.push_back(2);

? ? vecInt.push_back(4);

? ? vecInt.push_back(7);

? ? std::sort(vecInt.begin(),vecInt.end());

? ? for(auto itt:vecInt)

? ? {

up和upper。? ? ? ? std::cout<<itt<<endl;

? ? }

? ? std::vector<int>::iterator it=lower_bound(vecInt.begin(),vecInt.end(),4);

? ? std::cout<<"lower bound: "<<*it<<endl;

? ? it=upper_bound(vecInt.begin(),vecInt.end(),4);

? ? std::cout<<"upper bound: "<<*it<<endl;

lower怎么用。? ? return 0;

}

輸出:

3.2 降序情況

? ? 降序情況一定要記得指定比較函數

#include<iostream>

什么upper?#include<algorithm>

#include<vector>

#include<iterator>

using namespace std;

static bool weightComp( int a, int b)//指定比較函數,重要事情說3遍

????{

caseupper。? ? ? ? return a>b;

? ? }

int main(int argc,char**argv)

{

? ? std::vector<int> vecInt;

? ? vecInt.push_back(1);

uppercasing、? ? vecInt.push_back(3);

? ? vecInt.push_back(19);

? ? vecInt.push_back(2);

? ? vecInt.push_back(4);

? ? vecInt.push_back(7);

? ? std::sort(vecInt.begin(),vecInt.end(),weightComp);//指定比較函數,重要事情說3遍

lower、? ? for(auto itt:vecInt)

? ? {

? ? ? ? std::cout<<itt<<endl;

? ? }

? ? std::vector<int>::iterator it=lower_bound(vecInt.begin(),vecInt.end(),4,weightComp);//指定比較函數,重要事情說3遍

? ? std::cout<<"lower bound: "<<*it<<endl;

lowerbound和upperbound,? ? it=upper_bound(vecInt.begin(),vecInt.end(),4,weightComp);//指定比較函數,重要事情說3遍

? ? std::cout<<"upper bound: "<<*it<<endl;

? ? return 0;

}

輸出:

lower和upper是什么意思?????好了,到了這里小伙伴還有什么不明白的嗎?可以聯系@我。在次總結一下,a. 使用這兩個函數前vector/list等要排序; b. 調用函數時默認是升序,降序記得指定并自己編寫比較函數. c. 引入數學半開半閉區間是很好的理解方式。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/172469.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息