Jump to content

  • Log In with Google      Sign In   
  • Create Account


C++11 hash containers


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Storyyeller   Members   -  Reputation: 212

Like
0Likes
Like

Posted 02 December 2011 - 11:31 AM

So how does one use the new unordered_set and unordered_map? I haven't been able to find a single code example of these containers being used with a user defined type. And it isn't clear how you extend std::hash, or if this is even what you're supposed to do.
I trust exceptions about as far as I can throw them.

Sponsor:

#2 way2lazy2care   Members   -  Reputation: 782

Like
0Likes
Like

Posted 02 December 2011 - 11:35 AM

So how does one use the new unordered_set and unordered_map? I haven't been able to find a single code example of these containers being used with a user defined type. And it isn't clear how you extend std::hash, or if this is even what you're supposed to do.


There's a usage example on wikipedia page for unordered_map o.O

Do you mean something different?

#3 Cornstalks   Crossbones+   -  Reputation: 6974

Like
0Likes
Like

Posted 02 December 2011 - 11:40 AM


So how does one use the new unordered_set and unordered_map? I haven't been able to find a single code example of these containers being used with a user defined type. And it isn't clear how you extend std::hash, or if this is even what you're supposed to do.


There's a usage example on wikipedia page for unordered_map o.O

Do you mean something different?


I think he means for user defined types, particularly how to generate the hash value for user defined types.

I'm not experienced with C++11 features, but I think it's probably similar to boost::unordered_map (read the last paragraph at the end of the page) where you have to extend boost::hash. My guess is the standard library works almost the same, except in the std namespace. Seeing as many of the C++11 features came from boost, this is my logical guess.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#4 Storyyeller   Members   -  Reputation: 212

Like
0Likes
Like

Posted 02 December 2011 - 11:51 AM

I tried to extend std::hash, but I always get the error "'hash' is not a template". I think it might be because I already have a namespace, but I'm not sure what I'm supposed to do.
I trust exceptions about as far as I can throw them.

#5 SiCrane   Moderators   -  Reputation: 9496

Like
0Likes
Like

Posted 02 December 2011 - 12:41 PM

Show what you've tried.

#6 Storyyeller   Members   -  Reputation: 212

Like
1Likes
Like

Posted 02 December 2011 - 02:07 PM

[source lang='cpp']#include <unordered_set>//other includesnamespace Symmetry{struct Offset { int y, x; };namespace std { template <> class hash<Offset>{ public : size_t operator()(const Offset& arg ) const { return 0; } };};inline bool operator == (const Offset& a, const Offset& b) {return a.y == b.y && a.x == b.x;}}[/source]

I also tried changing std to ::std but that didn't help.
I trust exceptions about as far as I can throw them.

#7 SiCrane   Moderators   -  Reputation: 9496

Like
0Likes
Like

Posted 02 December 2011 - 02:38 PM

std is not a sub-namespace of Symmetry. Also, did you #include <functional>?

#8 Storyyeller   Members   -  Reputation: 212

Like
0Likes
Like

Posted 02 December 2011 - 02:40 PM

So how do I override std::hash while in a different namespace then? and no, I wasn't aware that <functional> was required.
I trust exceptions about as far as I can throw them.

#9 TheUnbeliever   Members   -  Reputation: 961

Like
0Likes
Like

Posted 02 December 2011 - 02:46 PM

So how do I override std::hash while in a different namespace then?


Just put it in the right namespace.

namespace Symmetry
{
  // stuff
}

namespace std
{
  // hash
}

[TheUnbeliever]

#10 SiCrane   Moderators   -  Reputation: 9496

Like
0Likes
Like

Posted 02 December 2011 - 02:47 PM


namespace Symmetry{

  struct Offset { int y, x; };

  inline bool operator == (const Offset& a, const Offset& b) {return a.y == b.y && a.x == b.x;}

}



namespace std {

  template <>

  class hash<Symmetry::Offset> {

  public:

    size_t operator()(const Symmetry::Offset & arg) const {

      return 0;

    }

  };

}



#11 Storyyeller   Members   -  Reputation: 212

Like
0Likes
Like

Posted 02 December 2011 - 02:50 PM

Well I was hoping for a method that wouldn't require splitting up my namespace and repeating the name several times, but from the responses it looks like there's no way around it. Oh well.
I trust exceptions about as far as I can throw them.

#12 Washu   Senior Moderators   -  Reputation: 4663

Like
0Likes
Like

Posted 02 December 2011 - 04:20 PM

Also, returning 0 for your hash is, generally speaking, a bad idea :)

I realize its just sample code, but even a simple xor is better than 0.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#13 Storyyeller   Members   -  Reputation: 212

Like
0Likes
Like

Posted 02 December 2011 - 04:23 PM

Yeah, that's obviously not my real hash function.
I trust exceptions about as far as I can throw them.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS