Hi!
Is there anything in C++14 that can be used to hash C like strings.
In C++17 I could use std::hash<std::string_view>.
On Sun, 13 Oct 2024 13:43:56 +0200
Marcel Mueller <news.5.maazl@spamgourmet.org> boring babbled:
Hi!
Is there anything in C++14 that can be used to hash C like strings.
In C++17 I could use std::hash<std::string_view>.
std::hash appeared in C++11 IIRC so not sure what the problem is.
I was digging through gcc's header files and came across how it
implements std::hash for various things. There's nothing in C++14 for
this,
but it shouldn't be rocket science to define a custom
specialization of std::hash that uses the underlying hash function on
top of a C-style string.
Am 13.10.24 um 18:36 schrieb Sam:
I was digging through gcc's header files and came across how it implements >> std::hash for various things. There's nothing in C++14 for this,
It seems so.
but it shouldn't be rocket science to define a custom specialization of
std::hash that uses the underlying hash function on top of a C-style string.
Well, I hoped that there are optimized, platform specific implementations.
Muttley@DastartdlyHQ.org writes:
On Sun, 13 Oct 2024 13:43:56 +0200
Marcel Mueller <news.5.maazl@spamgourmet.org> boring babbled:
Hi!
Is there anything in C++14 that can be used to hash C like strings.
In C++17 I could use std::hash<std::string_view>.
std::hash appeared in C++11 IIRC so not sure what the problem is.
The problem is finding what to hash. std::hash is not overloaded for a C- >style string represented by a lonely char pointer. In C++17 one can
construct a std::string_view, and there is an available std::hash overload >for that.
I was digging through gcc's header files and came across how it implements >std::hash for various things. There's nothing in C++14 for this, but it >shouldn't be rocket science to define a custom specialization of std::hash >that uses the underlying hash function on top of a C-style string.
Am 14.10.2024 um 10:21 schrieb Muttley@DastartdlyHQ.org:
#include <stdio.h>
#include <unordered_map>
using namespace std;
int main()
{
auto hashobj = hash<const char *>();
printf("String1: %lu\n",hashobj("hello"));
printf("String2: %lu\n",hashobj("world"));
return 0;
}
If you hash some strings with that equal strings usually would
get a different hash.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 547 |
Nodes: | 16 (2 / 14) |
Uptime: | 60:36:29 |
Calls: | 10,398 |
Calls today: | 6 |
Files: | 14,067 |
Messages: | 6,417,476 |
Posted today: | 1 |