Upper_bound () and lower_bound () Usage
# Include <iostream> # include <algorithm> // required header file using namespace STD; int main () {int point [10] = {2, 3, 7, 8 }; int TMP = upper_bound (point, point + 5, 7)-point; // press from small to large, 7. Which position of the array point can be inserted? printf ("% d \ n", TMP); TMP = lower_bound (point, point + 5, 7)-point; // The position where the array point can be inserted at least printf ("% d \ n", TMP); Return 0 ;}
Output:
4
2
Lower_bound:
Return> = the first position of the object, lower_bound (2) = 3, lower_bound (3) = 3
If the target object exists, it is the location of the target object. If the target object does not exist, it is the next location.
Upper_bound:
Return> the first position of the object, upper_bound (2) = 3, upper_bound (3) = 4
Whether or not it exists is the last position.
Upper_bound () and lower_bound () Usage