首页 > 解决方案 > 最接近值的二进制搜索向量 C++

问题描述

就像标题说的那样,我正在尝试使用二进制搜索方法在排序向量中搜索最接近的给定值并返回其索引。我尝试使用 lower/upper_bound() 但返回值是第一个或最后一个向量值,或“0”。下面是我已将温度和电压读入向量的 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;

任何建议将不胜感激。我相信问题可能在于向量按降序排序,但我需要顺序保持不变才能比较值。

感谢@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];
}

标签: c++vectorbinary-searchifstream

解决方案


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

相反,我会使用 std::upper_bound,因此您不必检查是否相等(在 double 上只是为了让它变得更糟)然后回退一个以获得另一个边界数据点,并计算哪个实际上更接近。除了一些边界检查:

#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,作为您当前工作点的具体反例。还要注意两个端点输入的重要性。

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


推荐阅读