So, simple enough question as I'm having a brain dead moment.. How can I override/replace std::hash to use Google's City Hash?
My current approach is to have a wrapper around std::string and then specialize std::hash<> for that. But…
So the library I use has an enum (say it's named LibEnum). I need to have an std::unordered_set of LibEnum, but I get compilation error that there is no specialized std::hash for it. I could easily write it and just return the number of value (first…
Let's say we have a snippet of code as follows
#include
#include
#include
int main() {
std::list myList = {4, 1, 3, 2};
std::unordered_map::iterator, std::string> myMap;
…
I have a class template C which inherits from std::string, and I want to make it hashable like a normal std::string. I have written the following code, and it compiles.
I wonder if it is the correct implementation. Since a cast from derived class…
Assume my_hasher is a hashing function object. Should the following be correct, according to the Standard?
my_type k;
assert(my_hasher{}(k) == my_hasher{}(k));
The cppreference states the above assertion is correct.
However, the Standard…
I have a class
class Base {
...
virtual size_t GetHash() = 0;
...
};
and a number of classes, inherited from Base, that override GetHash().
I want to use these classes as a key in unordered_map or unordered_set. Currently I achieve it by…
I would like to hash a class that has two private members e.g.:
foo.h
class Foo {
private:
std::string a;
std::string b;
public:
Foo (std::string a, std::string b);
bool operator==(const Foo& other) const;
…
So I've been reading a lot of articles, documentation pages, posts, benchmarks, etc., concerning the use of std::hash and its standard implementations.
Synopsis
Looking here it seems that std::hash will always return an std::size_t, which following…
I have a base Base class. The goal is to force specialize std::hash for std::shared_ptr with all the classes that inherit from Base.
I have tried the following approach with a dummy template parameter but the compiler error obviously complains that…
Do the following two return statements return the same thing?
class NonTrivialClass
{
public:
size_t hash() const
{
// variation 1
return std::hash::_Do_hash(my_val_);
// variation…
I needed a std::unordered_map with key a std::pair so I "stole" the following code:
template
inline void hash_combine(std::size_t & seed, const T & v)
{
std::hash hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed…
I was wondering what would be the optimal way to calculate the hash, considering that the values of ptime that are used as the key differ mostly in the hour and date (minutes and seconds are usually 0).
I have done this but I feel that it is quite…
I am having trouble with some code doing partial specialization of std::hash for one of my classes. Here's a self contained example.
EDIT: Thanks Piotr, I changed the code to include the fixes but the error remains.
#include
#include…
I'm trying to compile some software on FreeBSD 9.2 and have run into a problem. Not being a c++ developer myself, and apparently being unable to find the right bit of into in google, I've been unable to solve the issue.
The developer inserted…