Usage of lower_bound and upper_bound

1. The parameter sequences of the two should be sorted in ascending order;
2. Both functions use the binary method to find the position of the specified element in the sequence;
3. The return value type of the two functions is iterator;
4. When the When the searched value is not in the sequence, the return value should be inserted into the sequence in order, and the return value of the two functions is the same;
> For example: sequence v=[1,3,5,7,9] ,x=4,
> lower_bound(v.begin(),v.end(),x) - v.begin() = 2
> upper_bound(v.begin(),v.end(),x) - v. begin() = 2
    
5. When the value to be found is in the sequence, the return values ​​of the two functions are different;
6. lower_bound returns the first position in the sequence that is not less than (that is, greater than or equal to) the specified value, that is
> v[lower_bound(v.begin(),v.end(),x)] >= x
> v[lower_bound(v.begin(),v.end(),3)] = 3


7. upper_bound returns the sequence The first position in the line that is greater than the specified value, i.e.
> v[upper_bound(v.begin(),v.end(),x)] > x
> v[upper_bound(v.begin(),v.end(), 3)] = 5

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325746357&siteId=291194637