带公历的unordered_map [英] unordered_map with gregorian dates
本文介绍了带公历的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.
- 一个简单的解决方案是将其转换为
std::string
并将其存储.我可能想避免这种解决方案,因为使用字符串非常昂贵. - 我试图找到一些将日期导出为数字的函数,但是我只能阅读
day()
函数,并且不确定这是否真的合适. - 也许我可以计算出日期和参考日期之间的天数?
- 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. - 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. - 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屋!
查看全文