gpt4 book ai didi

c++ - pair vector 上的 lower_bound 有效,upper_bound 无效

转载 作者:行者123 更新时间:2023-11-30 02:41:22 35 4
gpt4 key购买 nike

我有一个排序的 vector 对。为了找到 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 作为它的第二个参数。

关于c++ - pair<int,int> vector 上的 lower_bound 有效,upper_bound 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28191632/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com