【发布时间】:2011-11-22 06:45:58
【问题描述】:
我正在尝试遍历字符串列表并查找给定字符在所述字符串中的位置。然后,我根据字符出现的位置/是否将字符串存储在给定的向量中。在循环完成执行之前,我在以下代码中遇到运行时错误。我已经检查了六次,似乎找不到任何问题。
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;
}
【问题讨论】: