문제

I have several strings 2000 - 3000 characters in length and I would like to hash every substring with length X to an unordered_multimap. Thus I am traversing through each string character by character to determine each hash. The substr function creates a new string and so does inserting a std::pair into the multimap. I'd like to avoid as much as possible. Is there a way to work around this?

Pseudo Code Example:

For each String str:
    For i to str.length - hashlength
        hash = str.substr(i, hashlength) //A
        unordered_multimap.insert({{hash, i}}); //B

I would prefer to have parts A and B use as little constructor calls as possible.

도움이 되었습니까?

해결책

There are several libraries that allow you to do this. Such as boost::string_ref and llvm::StringRef. A similar class, string_view (upon which boost::string_ref is based), is in the works for future standardization. If you don't want to download another library, the class is fairly simple to implement. It is little more than a const char* to indicate the start of the sub-string, and an integer to indicate the length (alternatively, another pointer to indicate the end point), plus some utility functions.

A common thing to be aware of with all these classes, is that you need to make sure that the source string remains alive and unmodified (or at least, ensure no reallocations happen) for as long as the referencing object is used. In other words, treat them with the same care as you would a pointer (since that's essentially what they are).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top