C++11 hash containers
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.
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.
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.
[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.
#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.
So how do I override std::hash while in a different namespace then? and no, I wasn't aware that <functional> was required.
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]
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
Popular Topics
Advertisement