This article is senlie original, reproduced Please retain this address: http://blog.csdn.net/zhengsenlie
Upper_bound (used in ordered intervals)
Certificate -------------------------------------------------------------------------------------------------------------------------------------------------
Description: When upper_bound successfully finds a value,
Returns an iterator pointing to the next position of each element "not greater than value", instead of the iterator pointing to value,
Or cannot be found, return the location where the value should exist
Ideas:
1. loop until the Interval Length is 0
2. If value <* middle, continue searching in the first half
3. If value> = * middle, continue searching in the second half (when the value is equal, it will continue searching in the second half, so we can ensure that upper bound is found)
Source code:
Template <class forwarditerator, class T> inline forwarditerator upper_bound (forwarditerator first, forwarditerator last, const T & Value) {return _ upper_bound (first, last, value, distance_type (first ), iterator_category (first);} // release template <class forwarditerator, class T, class distance> forwarditerator _ upper_bound (forwarditerator first, forwarditerator last, const T & value, distance *, forward_iterator_tag) {distance Len = 0; distance (first, last, Len); distance half; forwarditerator middle; while (LEN> 0) {half = Len> 1; middle = first; advance (middle, half); If (value <* middle) Len = half; else {First = middle; ++ first; len = len-half-1 ;}} return first;} // release template <class randomaccessiterator, class T, class distance> randomaccessiterator _ upper_bound (randomaccessiterator first, last, const T & Value, distance *, random_access_iterator_tag) {distance Len = last-first; distance half; randomaccessiterator middle; while (LEN> 0) {half = Len> 1; middle = first + half; If (value <* middle) Len = half; else {// because the values greater than are searched in the second half, the final result must be upper bound, if value exists, first indicates the next position of value first = middle + 1; Len = len-half-1 ;}} return first ;}
Example:
int main(){ int A[] = { 1, 2, 3, 3, 3, 5, 8 }; const int N = sizeof(A) / sizeof(int); for (int i = 1; i <= 10; ++i) { int* p = upper_bound(A, A + N, i); cout << "Searching for " << i << ". "; cout << "Result: index = " << p - A << ", "; if (p != A + N) cout << "A[" << p - A << "] == " << *p << endl; else cout << "which is off-the-end." << endl; }}/*The output is:Searching for 1. Result: index = 1, A[1] == 2Searching for 2. Result: index = 2, A[2] == 3Searching for 3. Result: index = 5, A[5] == 5Searching for 4. Result: index = 5, A[5] == 5Searching for 5. Result: index = 6, A[6] == 8Searching for 6. Result: index = 6, A[6] == 8Searching for 7. Result: index = 6, A[6] == 8Searching for 8. Result: index = 7, which is off-the-end.Searching for 9. Result: index = 7, which is off-the-end.Searching for 10. Result: index = 7, which is off-the-end.*/