Search code examples
c++c++11substr

c++ efficiently get substring of string with index


In my project i have to iterate over a large string starting from index=0 and get length k substring. I've implemented string::substr() and wonder if there are other efficient methods.

For example :

std::string S ="ABCDEFGHIJKLMN"

i need to get all substrings of length =5 starting from the beginning of S.Just like "ABCDE", "BCDEF", "CDEFG" and so on..

My implementation is like below:

    void geekfunc(std::string &str)
{
    unsigned int index=0;
    for (; index<=(str.size()-K);++index)
    {
        ++myseqmap[str.substr(index,K)];
    }
}

This function is being called ten million times and I welcome other methods to try.


Solution

  • If you are using C++17, you can use string_view as your parameter and map key type. This way you won't make copies of the string contents every time you call substr. Just make sure the string you pass to the function is not destroyed or modified while your map it still in use.

    std::map<std::string_view, std::size_t> myseqmap;
    
    void geekfunc(std::string_view str)
    {
        unsigned int index=0;
        for (; index<=(str.size()-K);++index)
        {
            ++myseqmap[str.substr(index,K)];
        }
    }