samedi 24 août 2019

How to check if elements of std::vector

I have a very large std::vector v of type std::vector<std::string> v. Now I want to compare which of the elements in the vector start with a certain substring str. What is the fastest possible way to do that?

I was thinking of a for-loop that iteratively compares the start of each element of v with the substring str. I first tried

std::string substring = "bla";
for (long unsigned int i = 0; i < v.size(); i++)
{
    if (!strncmp(v[i].c_str(), substring.c_str(), substring.size())) 
    {
        std::cout << "Item found: " << v[i] << std::endl;
    }
}

Which is mixed with and I am not happy with that.

What better alternatives are there?

Aucun commentaire:

Enregistrer un commentaire