mascai
mascai

Reputation: 1882

Custom structure as key in unordered_map

I want to use structure Item as key in unordered_map I implemented structures for hash and equal operations. But I have an error

hashtable_policy.h:1384:16: error: no match for call to '(const ItemHash) (const Item&)' solution.cpp:28:12: note: candidate: 'size_t ItemHash::operator()(Item)' 32 | size_t operator()(Item item) { solution.cpp:28:12: note: passing 'const ItemHash*' as 'this' argument discards qualifiers

How can I fix the error?

My code

vector<int> get_key(const string& s) {
    vector<int> res(26, 0);
    for (int i = 0; i < s.size(); ++i) {
        int pos = s[i] - 'a';
        res[pos]++;
    }
    return res;
}

struct Item {
    vector<int> v;
    
    Item(const vector<int>& vec) : v(vec) {}
    bool operator==(Item other) {
        if (v.size() != other.v.size()) {
            return false;
        }
        for (int i = 0; i < v.size(); ++i) {
            if (v[i] != other.v[i]) {
                return false;
            }
        }
        return true;
    }
};

struct ItemHash {
    size_t operator()(Item item) {
        auto vec = item.v;
        size_t seed = vec.size();
        for(auto& i : vec) {
            seed ^= i + 0x9e3779b9 + (seed << 6) + (seed >> 2);
        }
        return seed;
    }
};

struct ItemEqual {
    bool operator()(Item item1, Item item2) {
        return item1 == item2;
    }  
};

vector<vector<int> > Solution::anagrams(const vector<string> &A) {
    vector<vector<int>> res;
    unordered_map<Item, vector<int>, ItemHash, ItemEqual> hash; // hash_key, vector of indexes
    for (int i = 0; i < A.size(); ++i) {
        Item item = Item(get_key(A[i]));
        hash[item].push_back(i);
    }
    for (const auto& i : hash) {
        res.push_back(i.second);
    }
    return res;
}

Upvotes: 0

Views: 358

Answers (1)

lubgr
lubgr

Reputation: 38325

You need to const-qualify ItemHash::operator()(Item), i.e.,

struct ItemHash {
    size_t operator()(Item item) const
    //                            ^^^^ here
    {
       // as before...
    }

Note that as @JeJo pointed out in the comments, you need the same fix for ItemEqual.

Upvotes: 2

Related Questions