带有格里高利日期的unordered_map

Abr*_*ile 5 c++ boost-date-time boost-unordered

我想存储boost::gregorian::date为一个键,boost::unordered_map但我无法编译代码,因为它缺少这个类的正确哈希函数.

  1. 一个简单的解决方案是转换std::string并存储它.我可能想避免这种解决方案,因为使用字符串非常昂贵.
  2. 我试图找到一些函数将日期导出到数字,但我只能阅读该day()函数,我不确定这是否真的合适.
  3. 也许我可以计算出我的约会和参考日期之间的天数?

有没有其他更好的方法来存储日期或功能导出日期作为数字?

Max*_*kin 8

为它实现哈希函数:

namespace boost { namespace gregorian {

inline size_t hash_value(date const& date)
{
    return boost::hash_value(date.julian_day());
}

} } // boost::gregorian
Run Code Online (Sandbox Code Playgroud)

julian_day 简直就是朱利安时代开始以来的日间指数(无论是什么).