lower_bound在 pair<int,int> 的向量上有效,upper_bound不起作用

lower_bound on a vector of pair<int,int> works, upper_bound does not

本文关键字:int bound 不起作用 有效 向量 upper gt lt lower pair      更新时间:2023-10-16

我有一个对的排序向量。为了找到lower_bound和upper_bound,我使用了:

bool cmp2(const pair<int,int> &p1, const int v)
{
    if(p1.first<v)
        return true;
    else
        return false;
}
auto it1 = lower_bound(V.begin(), V.end(), h, cmp2);
auto it2 = upper_bound(V.begin(), V.end(), h, cmp2);

这里 h 是一个整数。让我感兴趣的是,问题只存在于upper_bound。如果我注释掉upper_bound,我的程序就会成功构建。这可能是什么原因呢?

upper_bound调用具有其他顺序的参数的比较器(首先搜索值,然后从它正在搜索的范围内搜索元素)。

所以你的比较器不起作用,因为它需要一对作为它的第一个参数和一个int作为它的第二个参数。