I am new to python, and I am trying to convert some python code to C++.
path is a string, WORDS is a list of strings.
python snippit:
suggestions = filter(lambda x: x[0] == path[0] and x[-1] == path[-1], WORDS)
Because I do not know python at all, I have trouble understanding the notion of 0 and -1. Do they represent indexes of these arrays, like i
and i-1
would in C++?
Here is my attempt to convert:
vector<string>suggestions;
int len = path.length();
for (string &x : dictionary) //for each word in some dictionary
for (int i = 0; i < len; i++)
{
if (path[i] == x[i] && path[i-1] ==x[i-1])
suggestions.push_back(x);
break;
}
Even though I think it should be right, this code isn't doing what I want it to do. The filtered list from the python code is much shorter than the vector from the C++ code.