将用户输入与存储在向量中的值进行比较

时间:2016-06-07 23:13:28

标签: c++ debugging vector user-input findandmodify

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

  }
}

2 个答案:

答案 0 :(得分:0)

除了带有问号的部分外,您还有更多工作要做。即使您设法实现了替换部件,您的代码仍然无效。

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

find()搜索前两个参数给出的序列,即开始和结束迭代器值。这些是您的bad_wordsfind()检查第一个参数给出的值的第一次出现,并返回引用第一个找到的值的迭代器,如果找不到值则返回end()

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

但是,如果您在say_back中输入“绝对软糖”,find()当然不会找到它。因为你的bad_words都没有包含“绝对软糖”。 find()搜索完全匹配。

所以,如果您希望“替换”say_back字符串中的任何不良单词,这将无效。

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

在您正确实施搜索算法之前,找出如何替换bad_words中找到的内容的方式,就是说,将推车放在马前。

你需要先弄明白这一点;如果需要,您可以随时talk to your rubber duck

答案 1 :(得分: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;
        }

    }
  }

}