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;

3
  • I think this question and answer describes what you are looking for: stackoverflow.com/q/17016175/777186 (this assumes C++11, not TR1, though -- is that relevant?) Commented Nov 27, 2013 at 3:09
  • Thanks, that was helpful. Please re-read my question if you can, as I ran into a new problem. Commented Nov 27, 2013 at 3: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? Commented Nov 27, 2013 at 3:46

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

+1 on catching that. I think that solves the OP's problem unless something else popped up we've not seen yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.