C++11 hash containers

Started by
11 comments, last by Storyyeller 12 years, 4 months ago
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.
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?

[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.
[size=2][ 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 ]
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.
Show what you've tried.
[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.
I trust exceptions about as far as I can throw them.
std is not a sub-namespace of Symmetry. Also, did you #include <functional>?
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.

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]
[TheUnbeliever]

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

This topic is closed to new replies.

Advertisement