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

标签 c++ vector binary-search ifstream

正如标题所说,我正在尝试使用二进制搜索方法在排序 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/

相关文章:

具有最大 N 个相等元素位置的 C++ vector

c - 在 C 中用 fork 搜索

algorithm - Binary Search 可以/Is Binary Search 是一种贪心算法吗?

c++ - 如何在异常中抛出有意义的数据?

c++ - 如果调试运行正常但发布崩溃怎么办

r - 当您提前不知道长度时如何在 R 中创建向量?

c++ - 使用 vector 迭代器访问方法

arrays - 使用二进制搜索的三元组总和

c++ - VS 2008 - 标题的某些部分不包括在内?

c++ - C++ sprintf删除未定义的行为