到目前为止,这是我的程序。它可以编译,但会卡住并在最后一部分崩溃。我想重复用户的字符串输入,并用“ ****”替换在字符串中找到的所有不良词。我的错误很可能是在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;
}
}
}
好了,感谢我的朋友Ivan Drago,我得以解决这个问题。
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";
cout << "Push enter twice when done.\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) {
for (unsigned int i = 0; i < v1.size(); i++) {
for (unsigned int j = 0; j < v2.size(); j++) {
if (v1[i] == v2[j]) {
v2[j] = sub;
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句