in this vedio why “-1” is there ?
In this vedio why "-1" is there?
Hello @mehulbhandari358,
The function will return the iterator the required element and subtracting the starting element will give it’s position in the array.
But, to access the element you need to know the index at which it is present.
The relationship between position of element and it’s index in an array/vector is:
index = position -1
Thus, we subtract 1 in the video.
Suggestion:
Re-watch the video, you’ll understand it better now.
Hope, this would help.
Give a like if you are satisfied.
when we subtract the starting element from the required position , we get index.
this is given in the vedio
Hello @mehulbhandari358,
No, it returns the position and subtracting 1 will give the index.
Important points:
- Index starts from 0 till n-1
- Position range between 1 to n.
Example:
arr: 1 2 3 4 5
pos: 1 2 3 4 5
idx: 0 1 2 3 4
Suggestion:
Write a code and verify this for each element of the array.
Hope, this would help.
Give a like if you are satisfied.
i watch the previos vedio in that vedio it is mentioned that it returns index
in the binary search stl vedio
cout<<lb-arr; <---- gives the index