0

到目前为止,这是我的程序。它编译但在最后一部分卡住并崩溃。我想重复用户的字符串输入,并用“****”替换字符串中发现的任何坏词。我的错误很可能在 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;
    }

  }
}
4

2 回答 2

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;
        }

    }
  }

}
于 2016-06-08T12:59:34.970 回答
0

你有更多的工作要做,而不仅仅是带问号的部分。即使您设法实现了替换部件,您的代码仍然无法工作。

find(bad_words.begin(), bad_words.end(), say_back) != bad_words.end())

find()搜索由前两个参数给出的序列,开始和结束迭代器值。这些是你的bad_wordsfind()检查由第三个参数给出的值是否第一次出现,并返回引用第一个找到的值的迭代器,或者end()如果没有找到该值。

因此,如果 bad_words 包含“Fudge”,并且您在 中输入“Fudge” say_backfind()就会找到它。

但是,如果您在 中输入“Definitely Fudge” say_backfind()当然不会找到它。因为你没有一个bad_words包含“绝对软糖”,确切地说。find()搜索完全匹配。

因此,如果您希望“替换在say_back字符串中找到的任何坏词”,这将行不通。

在您开始考虑替换任何bad_wordsin之前say_back,您需要找出正确的算法。您需要找到 中的每个单词say_back,然后检查 中的每个单词bad_words

在您能够正确实施搜索算法之前,弄清楚如何替换您找到的东西,从某种意义上bad_words说,就是本末倒置。

您需要先弄清楚这一点;如果需要,您可以随时与您的橡皮鸭交谈

于 2016-06-07T23:27:59.810 回答