A. Huber
A. Huber

Reputation: 11

Why I get an error when implementing this easy hash for vector <int>?

I'm writing a program where I need to hash 10 elements and keep an int for every vector<int>.

So I've declared the undordered_map like this :

struct myhash {
    size_t operator()(const vector<int>& v) const {
        size_t value = 1;
        for (auto x: v) {
            value = value * P + hash<int>()(x);
        }
        return value;
    }
};
tr1::unordered_map < vector <int>, int, myhash > H;

I get this errors in struct myhash :

expected initializer before ':' token
expected  primary-expression before 'return'
expected ';' before 'return'

Any ideas ?

Upvotes: 0

Views: 94

Answers (1)

Columbo
Columbo

Reputation: 61009

Apparently your compiler has either

  • ...its C++11 support not enabled. GCC and Clang do this via -std=c++11.

  • ...no support for range-based for at all. In this case you have to stick to a traditional loop using iterators instead.

    for (std::vector<int>::const_iterator iter = v.begin();
         iter != v.end(); ++iter)
    {
        value = value * P + hash<int>()(*iter);
    }
    

Upvotes: 2

Related Questions