STL algorithm algorithms lower_bound and upper_bound (31)

Source: Internet
Author: User

Lower_bound prototype:

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);
This function returns the first value not less than (greater than or equal to) specified Val in the range.

If the values in the sequence are smaller than Val, the return value is last.

The sequence should be ordered!

Use operator <to compare the sizes of the two elements.

This function optimizes the comparison times of non-continuous storage sequences.

The behavior is similar:

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;}
A simple example:

#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;}
Run:




Upper_bound prototype:

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);

This function returns the first value in the range greater than the specified Val.

If the values in the sequence are smaller than Val, the return value is last.

The sequence should be ordered!

Use operator <to compare the sizes of the two elements.

This function optimizes the comparison times of non-continuous storage sequences.

The behavior is similar:

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;}
A simple example:

// 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;}
Running result:



The values returned by lower_bound and upper_bound are exactly the two values corresponding to interval _range!


------------------------------------------------------------------

// For more instructions on writing errors or poor information, you can leave a message below or click the email address in the upper left corner to send an email to me, pointing out my errors and deficiencies, so that I can modify them, thank you for sharing it.

Reprinted please indicate the source: http://blog.csdn.net/qq844352155

Author: unparalleled

Email: [email protected]

Yu gdut

------------------------------------------------------------------






STL algorithm algorithms lower_bound and upper_bound (31)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.