I was practicing through unordered_multimaps and I came across a problem of having an unordered_multimap containing another unordered_multimap.The compiler throws an error saying c++ standard does not provide hash of this type.I guess I have to write a hash function but my understanding is limited since I am new to STL.
I have already tried something like inserting a struct or another multimap to the unordered_multimap but no luck so far.
std::unordered_multimap<long,long>m_Map1;
std::unordered_multimap<CString,m_Map1>m_Map2; //This line throws
error
//inserting to the map
m_Map1.insert(std::pair<long,long>(10,20));
m_Map2.insert(_T("ABC"),m_Map1);
//also the compiler does not let me create an object for this map
m_Map1 m_ObjMap; //error here as well
How should I implement this.What I am trying to achieve here is one person's Name associated with birthdate and date on which he dies.I was hoping of having the dates in one map and mapping it with name to the m_Map2.
Your problem is that there is no specialisation of
std::hash
available forCString
Boiling the problem down to its simplest part, this will also not compile:
Because
std::unordered_multimap<CString, anything>
requires that there exists a classstd::hash<CString>
which providesstd::size_t operator()(CString const&) const
(it also requires an implementation ofstd::equal_to<CString>
but this is automatically available ifCString
supportsoperator==
.You can create such a class and legally inject it into the std namespace: