1

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;

Will Nasby
  • 1,068
  • 2
  • 16
  • 39
  • I think this question and answer describes what you are looking for: http://stackoverflow.com/q/17016175/777186 (this assumes C++11, not TR1, though -- is that relevant?) – jogojapan Nov 27 '13 at 03:09
  • Thanks, that was helpful. Please re-read my question if you can, as I ran into a new problem. – Will Nasby Nov 27 '13 at 03:30
  • +1 just for the great name for the hash functor =P (`hashbrowns` made me chuckle). Well, ok, the question itself is pretty well presented too. That aside, it looks like you're on the right track for providing a hash-function type to your unordered map, especially with the syntax error woolstar found. Were you able to finally get this to work, or did you need some additional help? – WhozCraig Nov 27 '13 at 03:46

1 Answers1

1

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

woolstar
  • 5,063
  • 20
  • 31
  • +1 on catching that. I *think* that solves the OP's problem unless something else popped up we've not seen yet. – WhozCraig Nov 27 '13 at 03:48