简体   繁体   English

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

[英]What is the difference between std::lower_bound and std::upper_bound?

The code below does not compile with both MSVC2017 and GCC11:下面的代码不能同时编译 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;
}

But if I replace std::lower_bound with std::upper_bound it starts to compile.但是如果我用std::upper_bound替换std::lower_bound ,它就会开始编译。 What is the difference?有什么不同?

error: no match for 'operator<'错误:“运算符<”不匹配

This sort of error suggests that some template code is trying to use an operator you haven't defined.这种错误表明某些模板代码正在尝试使用您尚未定义的运算符。

lower_bound and upper_bound do opposite comparisons. lower_boundupper_bound做相反的比较。 < (const TimePoint & dt, const PricePoint & a) is fine for upper_bound , but lower_bound needs you to define this: < (const TimePoint & dt, const PricePoint & a)适合upper_bound ,但lower_bound需要你定义这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关于std :: lower_bound和std :: upper_bound的问题 - Questions about std::lower_bound and std::upper_bound std :: lower_bound和std :: upper_bound的基本原理? - rationale for std::lower_bound and std::upper_bound? C++ 中 Vector 的 std::upper_bound 和 std::lower_bound 的复杂度是多少? - What is the complexity for std::upper_bound and std::lower_bound for Vector in C++? "stl中upper_bound和lower_bound的区别" - Difference between upper_bound and lower_bound in stl 我需要将什么类型的 std::function 分配给 std::upper_bound 或 std::lower_bound - What type of std::function to I need to be able to assign it to either std::upper_bound or std::lower_bound std :: upper_bound和std :: lower_bound的不同比较签名 - different compare signature for std::upper_bound and std::lower_bound 不包含密钥时,std :: map :: lower_bound或std :: map :: upper_bound? - std::map::lower_bound or std::map::upper_bound when the key is not contained? std::lower_bound 和 std::upper_bound [on hold] 的时间复杂度 - Time complexity of std::lower_bound and std::upper_bound [on hold] 这是使用std :: upper_bound和std :: lower_bound的正确方法吗? - is this right way to use std::upper_bound and std::lower_bound? 如何使用 lower_bound/upper_bound 从 std::set 获取索引号? - How to use lower_bound/upper_bound to get index number from std::set?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM