Garmaine Staff asked 1 year ago

I have two strings, i.e: APPLE and APPLEA. I want to iterate over APPLE and check if its characters belong to APPLEA. I have done this:

int counter=0;
for (j=0;j<dictionary[i].size();j++)
{
    if (word_to_match.find(dictionary[i][j]) != std::string::npos)
    {
        counter++;
    }
}

Where dictionary is just a std::vector that has APPLE and other words. Is it possible to avoid the for loop by using std::transform or another tool?

———————————-EDIT————————————

I have this, I dont know if it could be even cleaner

    std::for_each(dictionary[i].begin(),dictionary[i].end(),[&word_to_match,&counter](char const &c){
        if (word_to_match.find(c) != std::string::npos)
        {
            counter++;
        }
    });