[英]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_bound
和upper_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.