我试图通过一个字符串列表循环,并找到给定的字符在所述字符串中的位置。然后,我根据字符出现的位置/字符将字符串存储在给定的向量中。在循环结束执行之前,我在下面的代码中收到运行时错误。我已经查过了它六次,似乎无法找到任何错误。向量问题的向量
vector< vector<string> > p;
for(list<string>::iterator ix = dictionary.begin(); ix != dictionary.end(); ix++)
{
int index = contains(*ix, guess);
index++;
p.at(index).push_back(*ix); //0 will contain all the words that do not contain the letter
//1 will be the words that start with the char
//2 will be the words that contain the the char as the second letter
//etc...
}
int contains(string str, char c)
{
char *a = (char *)str.c_str();
for(int i = 0; i < (str.size() + 1); i++)
{
if(a[i] == c)
return i;
}
return -1;
}
而且,正如其他人在这里所指出的那样,在尝试捅过它之前,您应该确实填充p。 – Christopher