使用 STL 的映射中最大的下限绑定

Biggest inferior bound in a map using STL

本文关键字:绑定 STL 映射 使用      更新时间:2023-10-16

使用下面的代码,我可以找到某个值的最低上级界限。除了最大的劣势,我怎么能做同样的事情?

double Sup(double const x) const {
    //Lower bound: first element that is greater-or-equal.
    map<double,double>::iterator it=MapCurve.lower_bound(x);
    if (it!=MapCurve.end()) {
        return it->first;
    } else {
        --it;
        return it->first;
    }           
}   

假设您在地图上有这些键值:0.2 | 0.7 | 1.3 | 2.4 | 5.1

上(1.2) 给出 1.3

现在我想要 Inf 函数,使得 Inf(1.2) 将给出 0.7。 我该怎么做?

lower_bound(x)给出第一个不在x之前的元素(即>=)。你想要上一个元素。

auto it = MapCurve.lower_bound(x);
if (it == MapCurve.begin()) {
  // No element less than x in the map.
  // throw error or return error code.
}
--it;
return it->first;