到目前为止,这是我的程序。它编译但在最后一部分卡住并崩溃。我想重复用户的字符串输入,并用“****”替换字符串中发现的任何坏词。我的错误很可能在 find_Poop_inSentence 中。“调试断言失败。向量下标超出范围”
void find_Poop_inSentence(vector<string> & v1, vector<string> & v2, string sub);
int main()
{
cout << "Howdy partner, tell me some words you don't take kindly to.\n";
vector<string>bad_words;
string word;
while (cin >> word)
{
cin.ignore();
bad_words.push_back(word);
if (word == "exit")
break;
}
cout << "Ok partner, got it!\n";
cout << "Now say something and I'll repeat it back to you. Don't worry, I'll bleep out the words that you don't like.\n";
word = "";
vector<string> random_sentence;
while (cin >> word)
{
cin.ignore();
random_sentence.push_back(word);
if (cin.get() == '\n')
break;
}
find_Poop_inSentence(bad_words, random_sentence, "****");
cout << "You said: ";
for (unsigned int i = 0; i < random_sentence.size(); ++i) {
cout << ' ' << random_sentence[i];
}
system("Pause");
return 0;
}
void find_Poop_inSentence(vector<string> & v1, vector<string> & v2, string sub) {
int iterOne;
int iterTwo = 0;
int iteratorMax = v2.size();
for (iterOne = 0; iterOne < iteratorMax; iterTwo++) {
if (v1[iterOne] == v2[iterTwo]) {
v2[iterTwo] == sub;
}
if (iterTwo == iteratorMax ) {
iterOne++;
iterTwo = 0;
}
}
}