I need to extract all substring of a certain size from a given string and look-up each of them in a std::unordered_map<string, int*>
. I tried to use the suggestion from this answer and used std::less<>
as the comparator but the compiler (gcc 8.2) complains with the following error. I don't know if it even makes sense to fiddle with the comparator in case of an unordered_map
.
/afs/software/gcc/8.2.0/lssc0-linux/include/c++/8.2.0/bits/hashtable.h:195:21: error: static assertion failed: hash function must be invocable with an argument of key type
static_assert(__is_invocable<const _H1&, const _Key&>{}
c_counter.cpp: In function ‘void process(char*)’:
c_counter.cpp:158:27: error: no matching function for call to ‘std::unordered_map<std::__cxx11::basic_string<char>, int*, std::less<void> >::find(std::string_view&)’ if (counts->find(k) != counts->end()) {
The code is:
std::unordered_map<std::string, int*, std::less<>> *counts = new std::unordered_map<std::string, int*, std::less<>> ;
// stuff
void prcoess(char* r) {
std::string_view seq(r) ;
for (int i = 0 ; i <= l - 1 - 15 ; i++) {
string_view k = seq.substr(i, 15) ;
if (counts->find(k) != counts->end()) {
// do stuff
}
}
}
The whole point of using string_view
here is to avoid allocating memory and creating new strings for every substring, so is there any way that allows me to query the unordered_map
without undermining the optimization?