【发布时间】:2014-09-10 04:48:59
【问题描述】:
大家好,我遇到了 string.at(x) 超出范围的错误,我不知道为什么。任何帮助,基本上我试图确保我的对象字符串中的第一个字符不是 'z' 。另外我认为我的字符串比较可能无法正常工作,但如果我发现有重复的唯一单词,这可能与未完成的代码有更多关系。
struct wordCount{
string word;
int count;
}storeword[100];
void countWordFreq(wordCount compares[]){
int a=0;
unsigned i=0;
for(a;a<101;a++){
cout<<"Length"<<compares[a].word.length();
if(compares[a].word.at(i)<='z'||compares[a].word.at(i)>='A'){
compares[a].count++;
}
for(int b=1;b<101;b++){
cout<<"Length"<<compares[b].word.length();
if(compares[b].word.at(i)<='z'||compares[b].word.at(i)>='A'){
if(compares[a].word.compare(compares[b].word)==0){
cout<<"true" << endl;
compares[a].count++;
}
}
b++;
}
a++;
}
for(int q;/*compare[q].word.at(0)<='z'||compare[q].word.at(0)>='A'*/q<10;q++){
cout<<"Word: " << compares[q].word << " Count: " << compares[q].count << endl;
}
}
【问题讨论】:
-
我猜
compares数组的大小是100。如果是这样,数组索引计数器a和b需要小于100,而不是小于101。当a或b的值为100时,由于访问超出范围的内存,您将得到未定义的行为。