Jump to content
  • Advertisement
Sign in to follow this  
Storyyeller

C++11 hash containers

This topic is 2571 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

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?

Share this post


Link to post
Share on other sites

[quote name='Storyyeller' timestamp='1322847110' post='4889832']
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?
[/quote]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[source lang='cpp']
#include <unordered_set>
//other includes

namespace 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.

Share this post


Link to post
Share on other sites

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


Just put it in the right namespace.

[source]namespace Symmetry
{
// stuff
}

namespace std
{
// hash
}[/source]

Share this post


Link to post
Share on other sites

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;
}
};
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!