将用户输入与存储在向量中的值进行比较
Compare user input with values stored in vector
这是我到目前为止的程序。它编译但在最后一部分卡住并崩溃。我想重复用户的字符串输入并用“****”替换字符串中发现的任何坏词。我的错误很可能在 find_Poop_inSentence。 "Debug Assertion failed. vector subscript out of range"
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;
}
}
}
你要做的不仅仅是带问号的部分。即使您设法实现了替换部分,您的代码仍然无法正常工作。
find(bad_words.begin(), bad_words.end(), say_back) != bad_words.end())
find()
搜索前两个参数给出的序列,开始和结束迭代器值。这些是你的 bad_words
。 find()
检查第三个参数给出的值的第一次出现,return 迭代器引用第一个找到的值,或者 end()
如果没有找到该值。
因此,如果 bad_words 包含 "Fudge",并且您将 "Fudge" 键入 say_back
,find()
将找到它。
但是,如果您在 say_back
中输入 "Definitely Fudge",当然 find()
将找不到它。因为 bad_words
中的 none 恰好包含 "Definitely Fudge"。 find()
搜索完全匹配。
因此,如果您希望 "replace any bad words found in the" say_back
字符串,这将不起作用。
在您开始考虑替换 say_back
中的任何 bad_words
之前,您需要找出正确的算法。您需要找到 say_back
中的每个单词,然后检查 bad_words
.
中的每个单词
在您可以正确实施搜索算法之前,弄清楚如何替换您在 bad_words
中找到的东西,从某种意义上说,就是本末倒置。
你得先想清楚;如果需要,您可以随时 talk to your rubber duck.
感谢我的朋友 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;
}
}
}
}
这是我到目前为止的程序。它编译但在最后一部分卡住并崩溃。我想重复用户的字符串输入并用“****”替换字符串中发现的任何坏词。我的错误很可能在 find_Poop_inSentence。 "Debug Assertion failed. vector subscript out of range"
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;
}
}
}
你要做的不仅仅是带问号的部分。即使您设法实现了替换部分,您的代码仍然无法正常工作。
find(bad_words.begin(), bad_words.end(), say_back) != bad_words.end())
find()
搜索前两个参数给出的序列,开始和结束迭代器值。这些是你的 bad_words
。 find()
检查第三个参数给出的值的第一次出现,return 迭代器引用第一个找到的值,或者 end()
如果没有找到该值。
因此,如果 bad_words 包含 "Fudge",并且您将 "Fudge" 键入 say_back
,find()
将找到它。
但是,如果您在 say_back
中输入 "Definitely Fudge",当然 find()
将找不到它。因为 bad_words
中的 none 恰好包含 "Definitely Fudge"。 find()
搜索完全匹配。
因此,如果您希望 "replace any bad words found in the" say_back
字符串,这将不起作用。
在您开始考虑替换 say_back
中的任何 bad_words
之前,您需要找出正确的算法。您需要找到 say_back
中的每个单词,然后检查 bad_words
.
在您可以正确实施搜索算法之前,弄清楚如何替换您在 bad_words
中找到的东西,从某种意义上说,就是本末倒置。
你得先想清楚;如果需要,您可以随时 talk to your rubber duck.
感谢我的朋友 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;
}
}
}
}