Lower_bound () and Upper_bound ()

Source: Internet
Author: User

Header file:#include<algorithm>

Function Classification:Binary search (operating on partitioned/sorted ranges)

function function: Lower_bound () returns an iterator pointer to the first position where Val appears in the lookup sequence, and Upper_bound () returns an iterator pointer to the last position in which Val appears in this lookup sequence.

The function template function of Lower_bound () is equivalent to:

Template <classForwardIterator,classT>forwarditerator Lower_bound (ForwardIterator First, ForwardIterator Last, const t& val) {ForwardIteratorit; Iterator_traits<forwarditerator>::d Ifference_typeCount, 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;} 

upper_bound()function template function is equivalent to:

ForwardIterator Upper_bound (forwarditerator first, ForwardIterator last,Constt& val) {ForwardIterator it; Iterator_traits<forwarditerator>::d ifference_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; }ElseCount=Step; }returnFirst;}

Lower () returns the position of the first occurrence of Val in an ordered array, and returns the last position of the array if it is not found.

The upper () returns the last position in the ordinal array where Val has occurred, if not found, returns the last position of the array if Val is equal to the last element of the array, and returns the last position of the array.

Upper () except for one more than lower () = , the other places are exactly the same.

In the code, array is the one you want to find, B is the starting position of the arrays, E is the end of the array, Val is the value to be looked up, first is a pointer to a position in the array, the count is the number of bits that a pointer can move, and SETP is the number of digits required by the cursor.

Lower (), when Array[it] is more than a Val hour, it moves the first pointer and moves only one (that is, it guarantees that the position where Val is to be returned is where it was, and if array[it] is equal to Val, the primary pointer does not move. The value of count is always reduced, halved at a time, eventually equal to 0, the loop ends and then returns to first, if ARRAY[IT] is greater than Val, first does not move, and count is reduced to a size that makes array[it] less than or equal to Val. After moving, update the maximum number of bits that can be moved, which is the difference between E and first, and the right part of both.
In array a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} find 7
Call Lower (A, 0, 9, 7);
First = 0; Count = 9;
First cycle:
it = 0; Step = 4; it = 4; A[it] = 5; First = 5; Count = 4;

Second cycle:
it = 5; Step = 2; it = 7; A[it] = 8; First = 6; Count = 1;

Third cycle:
it = 6; Step = 0; it = 6; A[it] = 7; Count = 0;

Find the end.

#include <bits/stdc++.h>using namespace STD;intLowerint*Array,intBintEintVal//Two points to find the Nether{intfirst = b;intCount = E-b; while(Count >0)    {intit = first;intStep = Count/2; it = it + step;if(Array[It] < val) {first = ++it; Count = Count-(step +1); }Else{count = step; }    }returnFirst;}intUpperint*Array,intBintEintVal//Two-point lookup upper bound{intfirst = b;intCount = E-b; while(Count >0)    {intit = first;intStep = Count/2; it = it + step;if(Array[It] <= val) {first = ++it; Count = Count-(step +1); }Else{count = step; }    }returnFirst;}intMain () {int Array[] = {0,1,2,3,4,5,5,5,5,5,6,6,6,7,8,9,Ten, One, One, One, A, A, -, -};intE =sizeof(Array) /sizeof(int);intans1 = Lower (Array,0E1, One);intANS2 = Upper (Array,0E1, One);printf("The first occurrence of the array in the 11 position:%d\n", ans1);printf("The last position in the array where 11 has occurred:%d\n", ANS2);printf("------------------------------------------\ n");intANS3 = Lower (Array,0E1, -);intANS4 = Upper (Array,0E1, -);printf("The first occurrence of the array in the 13 position:%d\n", ANS3);printf("The last position in the array where 13 has occurred:%d\n", ANS4);//Returns the last position of the array here    printf("------------------------------------------\ n");intANS5 = Lower (Array,0E1, -);intANS6 = Upper (Array,0E1, -);printf("%d not found, returns the last position of the array \ n", ANS5);printf("%d not found, returns the last position of the array \ n", ANS6);printf("-------------\ n");return 0;}

Lower_bound () and Upper_bound ()

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.