lower_bound 在向量对 <int,int> 上有效,upper_bound 无效
lower_bound on a vector of pair<int,int> works, upper_bound does not
我有一个排序的向量对。为了找到 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
作为第二个参数。
我有一个排序的向量对。为了找到 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
作为第二个参数。