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

3
以下代码在MSVC2017和GCC11上都无法编译:
#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,它就开始编译了。两者有什么区别?

1个回答

9

错误: 没有为 'operator<' 定义运算符

这种类型的错误表明一些模板代码尝试使用您未定义的运算符。

lower_boundupper_bound 执行相反的比较。 < (const TimePoint & dt,const PricePoint & a) 对于upper_bound是可以的,但是lower_bound 需要您定义这个:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接