最近值的二分搜索向量 C++

C.M*_*ock 1 c++ vector binary-search ifstream

就像标题所说的那样,我正在尝试使用二分搜索方法来搜索排序向量中最接近的给定值并返回其索引。我尝试使用 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
Run Code Online (Sandbox Code Playgroud)

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

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];
}
Run Code Online (Sandbox Code Playgroud)

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

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;
Run Code Online (Sandbox Code Playgroud)

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

感谢@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];
}
Run Code Online (Sandbox Code Playgroud)

Ken*_*rom 5

由于您有一个非递增范围(按降序排序),您可以将 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

输出

-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
Run Code Online (Sandbox Code Playgroud)

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

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