STL upper_bound (), lower_bound () function learning + self-implementation

Source: Internet
Author: User

STL upper_bound (), lower_bound () function learning + self-implementation

In STL, these two functions are used to find the position of an element in an ordered array. In simple usage, upper_bound (begin, end, key) and start is the start point of the search, end is the end point, key is the key value, and lower_bound () is used in the same way. The upper_bound () function returns the first value greater than the value to be searched (or the next position of this element ), lower_bound is a location smaller than or equal to the keyword (or the location where the keyword appears for the first time ),

 

# Include

 
  
Using namespace std; const int maxn = 2222; int a [maxn]; int n; int my_upper_bound (int num) {int l = 0; int h = n-1; while (l <= h) {int mid = (l + h)/2; if (numa [mid]) l = mid + 1; else h = mid-1 ;} return l;} int main () {cin> n; for (int I = 0; I

  
   
> A [I]; printf ("my_upper_bound (4) at % d \ n", my_upper_bound (4); printf ("my_lower_bound (4) location in % d \ n ", my_lower_bound (4); printf (" lower_bound (4) location in % d \ n ", lower_bound (a, a + n, 4) -a); printf ("upper_bound (4) at % d \ n", upper_bound (a, a + n, 4)-a); return 0 ;}

  

 


 

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.