C++ lower_bound

Several times I have seen this kind of line in use of lower_bound function:
**

ll ind=lower_bound(p,p+n,arr[i])-p;

**
What this mean? Why here extra -p?
please anyone explain me this…

First Lower Bound Gives you iterator/pointer to the to location where element is present if present .
here p is an int array and p is a pointer which points to the first element of the arr
*p == p[0] ;

So subtraction of this tow pointers that is pointer given by lower bound and int pointer gives you the index of arr[i] in array p

int p[] = { 1 , 2 , 3 , 4 , 5 , 6 } ;
lower_bound(p,p+6,4)-p == (p+3)-p == 3 (index of 4)

This works only on Sorted array

Although it takes place in terms of adresses further which are converted in int form .

2 Likes

Thank you @rushi2001 . Now I understood it…

1 Like