Using unordered_map<size_t, unordered_set<string>> to find collisions

230 Views Asked by At

Could someone please help me to understand how can i use the temporary unordered_map<size_t, unordered_set> to find collisions parsing from these parameters of the function:

template <typename Hash>
int CollisionsCounter(const Hash& hasher, istream& text) {

}

This kind of problem has been discussed already in this thread but the provided solution based on unordered_set. My problem is tht i dont' understand if i should cast a "hasher" to size_t somehow or this "size_t" Key type refers to smth else? But if i am asked to use "hasher" as a key for this temporary U-map than how to do tht if there is size_t type ? I have only started to study hash-tables, unordered_map, so i apologize if my question is silly or sounds so. The full task looks like this:

#include <string>
#include <sstream>
#include <unordered_set>
#include <unordered_map>

using namespace std;

template <typename Hash>
int FindCollisions(const Hash& hasher, istream& text)
{


}

struct HasherDummy
{
    size_t operator() (const string& str) const {
        size_t res = 0;
        for (char c : str) {
            res += static_cast<size_t>(c);
        }
        return res;
    }
};

struct DummyHash
{
    size_t operator()(const string&) const {
        return 42;
    }
};

int main()
{

    DummyHash dummy_hash;
       hash<string> good_hash;

       {
           istringstream stream("I love C++"s);
           cout << FindCollisions(dummy_hash, stream) << endl;
       }

       {
           istringstream stream("I love C++"s);
           cout << FindCollisions(good_hash, stream) << endl;
       }

}
//Output:
//2
//0

I tried to implement the solution from the same question asked some time ago (the link has been provided) but so far it doesnt work properly.

0

There are 0 best solutions below