unordered_map with gregorian dates

穿精又带淫゛_ 提交于 2019-12-07 05:19:50

问题


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. 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?


回答1:


Implement the hash function for it:

namespace boost { namespace gregorian {

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

} } // boost::gregorian

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



来源:https://stackoverflow.com/questions/24084445/unordered-map-with-gregorian-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!