gpt4 book ai didi

c++ - 最接近值的二进制搜索 vector C++

转载 作者:行者123 更新时间:2023-11-28 01:25:25 24 4
gpt4 key购买 nike

正如标题所说,我正在尝试使用二进制搜索方法在排序 vector 中搜索最接近的给定值并返回其索引。我试图使用 lower/upper_bound() 但返回值是第一个或最后一个 vector 值,或者“0”。下面是我将温度和电压读入 vector 的 txt 文件。

1.4 1.644290    -12.5
1.5 1.642990 -13.6
1.6 1.641570 -14.8
1.7 1.640030 -16.0
1.8 1.638370 -17.1

这是我当前有效的线性搜索

double Convert::convertmVtoK(double value) const
{
assert(!mV.empty());
auto it = std::min_element(mV.begin(), mV.end(), [value] (double a, double b) {
return std::abs(value - a) < std::abs(value - b);
});
assert(it != mV.end());
int index = std::distance(mV.begin(), it);
std::cout<<kelvin[index];
return kelvin[index];
}

这是我目前正在努力提高性能的算法。

double Convert::convertmVtoK(double value)
{
auto it = lower_bound(mV.begin(), mV.end(), value);

if (it == mV.begin())
{
it = mV.begin();
}
else
{
--it;
}

auto jt = upper_bound(mV.begin(), mV.end(), value), out = it;

if (it == mV.end() || jt != mV.end() && value - *it > *jt - value)
{
out = jt;
}

cout<<"This is conversion mV to K"<<" "<< *out;

如有任何建议,我们将不胜感激。我认为问题可能出在按降序排序的 vector 上,但我需要顺序保持不变以便比较值。

感谢@John 解决了。对于将来需要此功能的任何人来说,这里是可行的。

double Convert::convertmVtoK(double value) const
{

auto it = lower_bound(mV.begin(), mV.end(), value, [](double a, double b){ return a > b; });
int index = std::distance(mV.begin(), it);
std::cout<<kelvin[index];
}

最佳答案

因为你有一个非递增范围(按降序排序),你可以使用 std::lower_bound 和大于运算符,如注释中所述。但是,这只会让您获得超过或等于您的数字的第一个结果。这并不意味着它是“最接近的”,这是您要求的。

相反,我会使用 std::upper_bound,这样您就不必检查是否相等(双重检查只是为了使情况变得更糟)然后退回一个以获得另一个边界数据点,并计算哪个是实际上更近了。连同一些边界检查:

#include <vector>
#include <algorithm>
#include <iostream>
#include <functional>
#include <iterator>

// for nonincreasing range of double, find closest to value, return its index
int index_closest(std::vector<double>::iterator begin, std::vector<double>::iterator end, double value) {
if (begin == end){
// we're boned
throw std::exception("index_closest has no valid index to return");
}

auto it = std::upper_bound(begin, end, value, std::greater<double>());

// first member is closest
if (begin == it)
return 0;

// last member is closest. end is one past that.
if (end == it)
return std::distance(begin, end) - 1;

// between two, need to see which is closer
double diff1 = abs(value - *it);
double diff2 = abs(value - *(it-1));
if (diff2 < diff1)
--it;
return std::distance(begin, it);
}

int main()
{
std::vector<double> data{ -12.5, -13.6, -14.8, -16.0, -17.1 };
for (double value = -12.0; value > -18.99; value = value - 1.0) {
int index = index_closest(data.begin(), data.end(), value);
std::cout << value << " is closest to " << data[index] << " at index " << index << std::endl;
}
}

输出

-12 is closest to -12.5 at index 0
-13 is closest to -12.5 at index 0
-14 is closest to -13.6 at index 1
-15 is closest to -14.8 at index 2
-16 is closest to -16 at index 3
-17 is closest to -17.1 at index 4
-18 is closest to -17.1 at index 4

请注意,例如-14 比 -14.8 更接近 -13.6,作为您当前工作点的特定反例。还要注意两端输入的重要性。

欢迎您从那里开始开尔文 [i]。当您不需要这样做时,我不喜欢使用外部数据数组作为函数的返回值,所以我只返回了索引。

关于c++ - 最接近值的二进制搜索 vector C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54065363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com