在 C++ 的双精度数组中搜索最近的值? [英] Search for nearest value in an array of doubles in C++?

查看:60
本文介绍了在 C++ 的双精度数组中搜索最近的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C++ 中有一个已排序的双精度数组.是否有 STL 函数可以将数组中最近值的索引返回给给定的双精度值?

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?

例如,给定以下数组

double myarray[5] = { 1.0, 1.2, 1.4. 1.5, 1.9 };

函数调用

search(myarray, 1.6);

应该返回 3,即最接近 1.6 的元素的索引,而不是 -1(或其他一些标志值),表示未找到值 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.

推荐答案

也许 std::lower_bound std::upper_bound 会帮助你.

这篇关于在 C++ 的双精度数组中搜索最近的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆