c++ - std::lower_bound 和 std::upper_bound 有什么区别?
问题描述
下面的代码不能同时使用 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
就会开始编译。有什么区别?
解决方案
错误:'operator<' 不匹配
这种错误表明某些模板代码正在尝试使用您尚未定义的运算符。
lower_bound
并upper_bound
进行相反的比较。< (const TimePoint & dt, const PricePoint & a)
很好upper_bound
,但lower_bound
需要你定义这个:
inline bool operator < (const PricePoint & a, const TimePoint & dt)
{
return dt < a.dt;
}
推荐阅读
- asp.net-core - 为什么 Newtonsoft [JsonIgnore] 停止在 asp core mvc 中工作
- regex - 16位身份证的正则表达式如何写,规则如下?
- javascript - 为什么 obj.constructor.prototype 并不总是等于 obj.__proto__?
- javascript - 如何将用于 React SPA 的 Paypal Javascript SDK 与后端服务器验证集成?
- macos - IDE 不尊重 macOS 中的环境变量
- c - 是否可以将结构指针转换为c中的函数指针?
- c# - 如何在 Blazor 中通过 Entity Framework Core 运行原始 SQL 查询
- firebase - 如何解决此错误“无法分配'用户'类型的值?' 键入“字符串?”?
- node.js - Electron js + Axios xhr.js:120 拒绝设置不安全的标头“user-agent”
- ios - 为什么我在 swift 中调用 getIndex(cell: TableViewCell1) 函数时出错?