首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >std::lower_bound和std::upper_bound有什么区别?

std::lower_bound和std::upper_bound有什么区别?
EN

Stack Overflow用户
提问于 2020-09-24 20:38:59
回答 1查看 85关注 0票数 3

下面的代码不同时使用MSVC2017和GCC11编译:

代码语言:javascript
复制
#include <deque>
#include <chrono>
#include <algorithm>

using Clock = std::chrono::system_clock;
using TimePoint = std::chrono::time_point<Clock>;

struct PricePoint
{
    TimePoint dt;
    double buy;
    double sell;
};

inline bool operator < (const TimePoint & dt, const PricePoint & a)
{
    return a.dt < dt;
}

int main()
{
    std::deque<PricePoint> priceSequence;
    const auto begin = std::lower_bound(priceSequence.begin(), priceSequence.end(), Clock::now());
    return 0;
}

但是,如果我将std::lower_bound替换为std::upper_bound,它将开始编译。有什么关系?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-24 20:45:46

错误:与“operator<”不匹配

这种错误表明某些模板代码试图使用未定义的操作符。

lower_boundupper_bound做了相反的比较。< (const TimePoint & dt, const PricePoint & a)适用于upper_bound,但是lower_bound需要定义如下:

代码语言:javascript
复制
inline bool operator < (const PricePoint & a, const TimePoint & dt)
{
    return dt < a.dt;
}
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64054070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档