0

I have a type Extended_Expression which I use in unordered sets, and with an equality declared like this:

bool ExtendedExpression::operator == (const ExtendedExpression & exp) const{ ... }

But the find operator of unordered_set does not seem to work, it seems that it is using another kind of equality, like memory adress.

What is the simplest way to specify the equality operator to be used ? I'm beginner in C++ sorry if the question is obvious.

D K
  • 133
  • 5
  • 1
    That looks fine for an equality operator. Maybe you haven't implemented a good hashing function. – juanchopanza Aug 28 '14 at 15:54
  • there is a Hash() defined but is it automatically used by unordered set ? In public: HashExpr Hash() const {return _hash;} and in protected: HashEpxr _hash; – D K Aug 28 '14 at 15:57

1 Answers1

0

You need to specialise std::hash for your for your type if you want unordered_set to work as you expect. For an example see the answer here https://stackoverflow.com/a/8157967/2558027.

All the new unordered containers, such as unordered_map are hash table based rather than based on a red-black tree as the traditional ones are. This allows extremely fast, constant time, lookup for larger data sets.

Community
  • 1
  • 1
sjdowling
  • 2,994
  • 2
  • 21
  • 31