Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The C++ Standard doesn`t provide a hash for this type

I am trying to create a unordered-map using a pair as a key. This is something new for me so I followed some tutorials and I wrote this:

    struct pair_hash {
        template <class T1, class T2>
        std::size_t operator () (const std::pair<T1, T2> &p) const {
            auto h1 = std::hash<T1>{}(p.first);
            auto h2 = std::hash<T2>{}(p.second);

            return h1 ^ h2;
        }
    };

int wmain(int argc, wchar_t * argv[])
{
   {...}

    using Key = std::pair<DWORD, DWORDLONG>;
    std::unordered_map<Key, USN, pair_hash> mymap;


    std::pair<DWORD, DWORDLONG> mypair(dwVolSN, fileId);

    mymap.insert({ mypair, usn });

    std::unordered_map<Key, USN>::const_iterator got;
    got = mymap.find(mypair); // HERE I GET THE ERROR

    return 0

}
like image 983
Denis Ilie-Ablachim Avatar asked Feb 05 '23 09:02

Denis Ilie-Ablachim


1 Answers

Try std::unordered_map<Key, USN,pair_hash>::const_iterator got;

or auto got = mymap.find(mypair);

like image 120
Kelvin Sherlock Avatar answered Feb 18 '23 08:02

Kelvin Sherlock