STL algorithm演算法lower_bound和upper_bound(31),stlalgorithm

來源:互聯網
上載者:User

STL algorithm演算法lower_bound和upper_bound(31),stlalgorithm

lower_bound原型:

function template<algorithm>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);
該函數返回範圍內第一個不小於(大於或等於)指定val的值。

如果序列中的值都小於val,則返回last.

序列應該已經有序!

使用operator<來比較兩個元素的大小。

該函數最佳化了比較非連續儲存序列的比較次數。

其行為類似於:

template <class ForwardIterator, class T>  ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val){  ForwardIterator it;  iterator_traits<ForwardIterator>::difference_type count, step;  count = distance(first,last);  while (count>0)  {    it = first; step=count/2; advance (it,step);    if (*it<val) {                 // or: if (comp(*it,val)), for version (2)      first=++it;      count-=step+1;    }    else count=step;  }  return first;}
一個簡單的例子:

#include <iostream>#include <vector>#include <algorithm>using namespace std;int main(int argv,char **argc){vector<int> v1{1,2,3,4};cout<<"v1=";for(int i:v1)cout<<i<<" ";cout<<endl;auto it=lower_bound(v1.begin(),v1.end(),3);cout<<"lower_bound(v1.begin(),v1.end(),3)="<<*it<<endl;auto it2=lower_bound(v1.begin(),v1.end(),5);if(it2==v1.end())cout<<"lower_bound(v1.begin(),v1.end(),5)=v1.end()"<<endl;elsecout<<"lower_bound(v1.begin(),v1.end(),5)="<<*it2<<endl;}
運行:




upper_bound原型:

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

該函數返回範圍內第一個大於指定val的值。

如果序列中的值都小於val,則返回last.

序列應該已經有序!

使用operator<來比較兩個元素的大小。

該函數最佳化了比較非連續儲存序列的比較次數。

其行為類似於:

template <class ForwardIterator, class T>  ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& val){  ForwardIterator it;  iterator_traits<ForwardIterator>::difference_type count, step;  count = std::distance(first,last);  while (count>0)  {    it = first; step=count/2; std::advance (it,step);    if (!(val<*it))                 // or: if (!comp(val,*it)), for version (2)      { first=++it; count-=step+1;  }    else count=step;  }  return first;}
一個簡單的例子:

// lower_bound/upper_bound example#include <iostream>     // std::cout#include <algorithm>    // std::lower_bound, std::upper_bound, std::sort#include <vector>       // std::vectorint main () {  int myints[] = {10,20,30,30,20,10,10,20};  std::vector<int> v(myints,myints+8);           // 10 20 30 30 20 10 10 20  std::sort (v.begin(), v.end());                // 10 10 10 20 20 20 30 30  std::vector<int>::iterator low,up;  low=std::lower_bound (v.begin(), v.end(), 20); //          ^  up= std::upper_bound (v.begin(), v.end(), 20); //                   ^  std::cout << "lower_bound at position " << (low- v.begin()) << '\n';  std::cout << "upper_bound at position " << (up - v.begin()) << '\n';  return 0;}
運行結果:



lower_bound和upper_bound返回的值剛好是equal_range對應的兩個值!


——————————————————————————————————————————————————————————————————

//寫的錯誤或者不好的地方請多多指導,可以在下面留言或者點擊左上方郵件地址給我發郵件,指出我的錯誤以及不足,以便我修改,更好的分享給大家,謝謝。

轉載請註明出處:http://blog.csdn.net/qq844352155

author:天下無雙

Email:coderguang@gmail.com

2014-9-17

於GDUT

——————————————————————————————————————————————————————————————————







c#中哪種資料結構有upper_bound()與lower_bound()函數?

沒有,要自己實現。
 
c++中 map裡upper_bound與lower_bound分別怎理解? 主要是想問關鍵字的上界與下界是什

是一個有序的序列,使用upper_bound時候,返回你想要尋找區間中最大值的迭代器~~lower_bound相反~~~
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.