Sign in to follow this  
Storyyeller

C++11 hash containers

Recommended Posts

Storyyeller    215
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
way2lazy2care    790
[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.
[/quote]

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
Cornstalks    7030
[quote name='way2lazy2care' timestamp='1322847333' post='4889833']
[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.
[/quote]

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 [url="http://www.boost.org/doc/libs/1_38_0/doc/html/unordered.html"]boost::unordered_map[/url] (read the last paragraph at the end of the page) where you have to extend [url="http://www.boost.org/doc/libs/1_38_0/doc/html/hash/custom.html"]boost::hash[/url]. 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
Storyyeller    215
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
Storyyeller    215
[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
TheUnbeliever    963
[quote name='Storyyeller' timestamp='1322858424' post='4889905']
So how do I override std::hash while in a different namespace then?
[/quote]

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
SiCrane    11839
[code]
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;
}
};
}
[/code]

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this