使用 STL 的地图中的最大下界
Biggest inferior bound in a map using STL
使用下面的代码我可以找到特定值的最低上界。除了最大的下界,我怎么能做同样的事情呢?
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
Sup(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;
使用下面的代码我可以找到特定值的最低上界。除了最大的下界,我怎么能做同样的事情呢?
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
Sup(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;