I have a sorted array of double values in C++. Is there an STL function that will return the index of the nearest value in the array to a given double value?
For example, given the following array
double myarray[5] = { 1.0, 1.2, 1.4. 1.5, 1.9 };
the function call
search(myarray, 1.6);
should return 3
, the index of the element nearest to 1.6, instead of -1
(or some other flag value) indicating that the value 1.6 wasn't found.
maybe std::lower_bound
std::upper_bound
will help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With