带公历的unordered_map [英] unordered_map with gregorian dates

查看:111
本文介绍了带公历的unordered_map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将boost::gregorian::date存储为boost::unordered_map的键,但是我无法编译代码,因为它缺少此类的适当哈希函数.

I would like to store boost::gregorian::date as key of a boost::unordered_map but I cannot compile the code as it is missing a proper hash function for this class.

  1. 一个简单的解决方案是将其转换为std::string并将其存储.我可能想避免这种解决方案,因为使用字符串非常昂贵.
  2. 我试图找到一些将日期导出为数字的函数,但是我只能阅读day()函数,并且不确定这是否真的合适.
  3. 也许我可以计算出日期和参考日期之间的天数?
  1. An easy solution would be converting to std::string and store it. I possibly would like to avoid this solution as using string is quite expensive.
  2. I tried to find some function exporting a date to number but I can read only the day() function and I am not sure if this is really suitable.
  3. Maybe I can calculate the number of days between my date and a reference date?

还有其他更好的方法来存储日期或将日期导出为数字的函数吗?

Is there any other better way to store date or a function exporting date as number?

推荐答案

为其实现哈希函数:

namespace boost { namespace gregorian {

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

} } // boost::gregorian

julian_day仅仅是朱利安时代开始以来的天数索引(无论是什么).

julian_day is simply the day index since Julian epoch start (whatever that is).

这篇关于带公历的unordered_map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆