How to store gregorian::date in an unordered_map?

I have a few lines of code.i need to use boost::gregorian::date as a key of unordered_map. I tried with boost::unordered_map but when i tried to map the date as a key it gives me an error like

typedef boost::unordered::unordered_map<boost::gregorian::date , int> unop;
boost::gregorian::date dt{2018,01,01};

unop[dt]=1;
error: expected unqualified-id before ‘[’ token

How can i solve this problem?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#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' ;
}

http://coliru.stacked-crooked.com/a/617034c0eed1bf8f
thank you very much.
how can i search a date exist or not in this unordered_map?
Use the find member function. For example:
1
2
3
const auto iter = unop.find(dt) ;
if( iter != unop.end() )
    std::cout << "found " << iter->first << "    mapped value is " << iter->second << '\n' ;

http://coliru.stacked-crooked.com/a/2cbde4ba4c2f7c5a
Topic archived. No new replies allowed.