1234567891011121314151617181920212223242526
#include <iostream> #include <string> #include <boost/unordered_map.hpp> #include <boost/date_time/gregorian/gregorian.hpp> // define a hash function for boost::gregorian::date struct gregorian_hash { std::size_t operator() ( const boost::gregorian::date& dt ) const { return boost::hash<std::string>{}( to_iso_string(dt) ) ; } }; int main() { // define a type alias (specify the hash function to be used) typedef boost::unordered::unordered_map<boost::gregorian::date , int, gregorian_hash > map_type ; // create an object map_type unop ; const boost::gregorian::date dt{2018,01,01}; unop[dt] = 1 ; unop [ { 1998, 7, 23 } ] = 22 ; for( const auto& pair : unop ) std::cout << to_simple_string( pair.first ) << ' ' << pair.second << '\n' ; }
123
const auto iter = unop.find(dt) ; if( iter != unop.end() ) std::cout << "found " << iter->first << " mapped value is " << iter->second << '\n' ;