Will Nasby
Will Nasby

Reputation: 1148

Hash struct using an object

I was given a piece of code to help me on an assignment, but I'm having trouble implementing it in my solution. Here's the piece of code:

#include<iostream>
#include<tr1/unordered_set>

using namespace std;
using std::tr1::unordered_set;


struct ihash
    : std::unary_function<int, std::size_t>
{


    std::size_t operator()(const int& x) const
    {
        return x;
    }
};

I have an object that I'd like to use to decide the size of the hashval I'd like to use. I came up with this:

/*int myhash(const Play & key, int tableSize){
    int hashval = 0;
    string keysize = key.getoTeam()+key.getdTeam();
    for(int i=0;i<keysize.length(); i++){
        hashval = hashval*5 + keysize[i];
    }
    return hashval;
}*/

But I can't find any code examples that use a struct for a hashtable that do something similarly, and the int version is not working with my declaration of my unordered_set. I declare it like so:

unordered_set<Play, myhash> PlaySet;

Can anyone help me connect the dots?

Update:

New error: main.cpp:38: error: expected unqualified-id before âreturnâ

I ended up with my has being:

struct hashbrowns{
    size_t operator()(const Play & a) const
    {
        string keysize = a.getoTeam()+a.getdTeam();
        size_t seed = 0;
        for(int i=0;i<keysize.length(); i++)
            seed = seed*5 + keysize[i];
    }
    return seed;
};

Line 38 being, return seed;

Upvotes: 1

Views: 103

Answers (1)

woolstar
woolstar

Reputation: 5083

In your updated code, your return needs to be a line higher. Right now its outside of the operator() function.

Upvotes: 1

Related Questions