使用向量对一些预定义的字符串发出哔哔声

Rap*_*ael 3 c++ vector

因此,我目前正在 Bjarne Stroustrup 的编程书籍“编程:使用 c++ 的原则和实践”中进行练习,但我目前只专注于一项练习。基本上,练习是编写一个程序,它会发出它不喜欢的单词。它的工作方式是用户输入一个字符串,程序重复这个词。如果用户输入的词是不喜欢向量的一部分,则该词将替换为“Bleep”。(我不知道我的解释是否正确,但理解起来应该不会太复杂)。

这是我的程序版本:

int main()
{
    string dislike = "Potato";
    string words = " ";

    cout << "Please enter some words: " << endl;
    while(cin>>words)
    {
        if(words==dislike)
        {
            cout << "Bleep!" << endl;
        }

        else
        {
            cout << words << endl;
        }
    }
    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,这个版本没有使用向量(它应该使用,因为练习就在本章中向量的解释之后)。所以我的问题是,如何实现一个包含许多“不喜欢”词的向量,如下所示:

vector<string>dislike;
dislike.push_back("Potatoes");
dislike.push_back("Peanuts");
dislike.push_back("Coconut");
Run Code Online (Sandbox Code Playgroud)

并使它像我的其他版本一样工作,没有向量(重复单词,但不喜欢的单词会发出哔哔声)。我似乎无法理解如何在向量中导航,以便它只会发出“不喜欢”的话。

如果有人可以帮助我并向我解释它是如何工作的(请不要只给我答案),我将不胜感激。

感谢您的时间和帮助,单独学习 C++ 并不总是那么简单,我感谢这个网站让我的学习曲线更容易一些。

博比酷

Ale*_*ler 5

好的,让我解释一个简单的方法。还有更优雅的,但现在重要的是让您了解如何std::vector访问以及如何正确组合控制结构。

步骤 1 - 遍历向量的所有元素

您可以使用迭代器遍历向量的所有元素。

for(vector<string>::const_iterator it = dislike.begin(); it != dislike.end(); ++it) {

   // now *it gives access to the current element (here: current dislike word)
   if (*it == words) {
       // ... yeah, we found out the current word is on the list!
   }         
}
Run Code Online (Sandbox Code Playgroud)

您可以通过调用 获得指向向量中第一个元素的迭代器begin(),然后继续递增 ( ++it) 直到到达向量的末尾。我const_iterator在这里使用是因为我不会修改任何元素,如果需要,请使用iterator.

也可以使用 a 进行std::vector索引[index](但通常不推荐):

for(size_t i = 0;i < dislike.size(); ++i) {
   // dislike[i] is the current element

   if (dislike[i] == words) {
      // huuuuray! another BEEEP candidate
   }
}
Run Code Online (Sandbox Code Playgroud)

第 2 步 - 尽早中断循环

一旦您确定我们有一个不喜欢的词,您就不需要进一步搜索向量。

for(vector<string>::const_iterator it = dislike.begin(); it != dislike.end(); ++it) {   
  if (*it == words) {
     // we found a positive match, so beep and get out of here
     cout << "Bleep!" << endl;
     break;
  }         
}
Run Code Online (Sandbox Code Playgroud)

第 3 步 - 如果我们已经处理过一个单词,请记下

bool is_beep = false;
for(vector<string>::const_iterator it = dislike.begin(); it != dislike.end(); ++it) {   
  if (*it == words) {
     // we found a positive match, so beep and get out of here
     cout << "Bleep!" << endl;
     is_beep = true;
     break;
  }         
}
// this is not a dislike word if is_beep is false, so print it as usual
if (!is_beep) {
   cout << words << endl;
}
Run Code Online (Sandbox Code Playgroud)

第 4 步 - 将它们放在一起

int main()
{
    vector<string>dislike;
    dislike.push_back("Potatoes");
    dislike.push_back("Peanuts");
    dislike.push_back("Coconut");
    string words = " ";

    cout << "Please enter some words: " << endl;
    while(cin>>words)
    {
        bool is_beep = false;
        for(vector<string>::const_iterator it = dislike.begin(); it != dislike.end(); ++it) {   
           if (*it == words) {
            // we found a positive match, so beep and get out of here
            cout << "Bleep!" << endl;
            is_beep = true;
            break;
          }         
        }
       // this is not a dislike word if is_beep is false, so print it as usual
       if (!is_beep) {
            cout << words << endl;
       }
    }
    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

查看std::find更惯用的解决方案 - 它基本上为您节省了内部循环。bool如果您稍微重新构建一下,您也可以在最后一个代码示例中摆脱它。我将把它留给你作为练习(提示:保持迭代器存活并在终止循环后检查它的值)。