I have created a simple array (not a vector, template, class...nothing of that sort) and I want to use a nice and neat binary search function from the stl to find a particular value in the array and return its index. Oh yes, the first thing I did is google "binary search function stl." And voila! So many results. But everything involves Random iterator, template, class, forward iterator etc and since I haven't learnt the OOPS part of C++ yet (Not that I am shying away from it, but I am just trying to put my NonOOP knowledge to work), I am unable to comprehend those complicated terms. Just like how for sorting an array, all I have to have is sort(Arr,Arr+N); with an algorithm header on top, could someone suggest a similar simple away of using the binary search function to find an element and return its index in this simple array of mine? asked 29 Nov '14, 18:50

The question has been closed for the following reason "The question is answered, right answer was accepted" by kunal361 01 Dec '14, 18:03
answered 29 Nov '14, 19:51
Hey @kunal361
(29 Nov '14, 21:19)
Begin and end are not for arrays...I feel...check this out...http://ideone.com/CWDWL1
(29 Nov '14, 22:03)
ohhkk thanks @kunal361
(29 Nov '14, 22:05)
Yes...it uses binary search....also u can go through upper_bound...it gives the last index of the key....lower_bound returns the first index!!!
(29 Nov '14, 22:09)

When rl=1 arr[l] is the largest element smaller than the key and arr[r] is the smallest element larger than the key....hope this helps...:) answered 30 Nov '14, 18:20
Of course it helped! :) Thank you soo sooo much! I asked this question because I wanted to implement this idea of binary search in a ZCO 2012 problem WORMHOLES (I've never used binary search in a question before, always pulled on with linear search) although I haven't completely got the problem right yet.
(01 Dec '14, 16:32)
Can u provide the problem link?? Also if this has answered your question...can I close this thread??
(01 Dec '14, 18:01)
1
I have posted it as a separate query as part of discussion on the same problem. It's here http://discuss.codechef.com/questions/56739/zco2012problemwormholes And yeah please close this thread.
(01 Dec '14, 18:03)

Thank you so much @kunal361 This is a kind of follow up question. Suppose the element ‘a’ is not present in my simple array. But instead, I am looking for an element ‘b’ which is nearest to ‘a’ and just less than ‘a’. Also, I am looking for an element ‘c’ which is nearest to ‘a’ and just more than ‘a’. How do I tweak binary search to implement this? I don't need an STL function, but a general procedure as I am unable to understand the behaviour of the mid value in the binary search implementation to make sure the above thing works. answered 30 Nov '14, 17:38

Thanks for asking this question, was just searching for it on internet...