使用循环重新分配向量中的字符串

reassigning strings in a vector using loops

本文关键字:向量 字符串 分配 循环 新分配      更新时间:2023-10-16

我是一个学习c++的初学者,试图编写一个程序,将单词读入向量,用"BLEEP"替换"坏"单词,并在向量被审查后打印出来。这是我认为我可能犯错误的地方。。。嵌套循环-如果在一段时间内words.size-应该是words.length吗?

任何见解都值得赞赏

#include "../../std_lib_facilities.h"
int main()
{
    cout << "type in text";
    vector<string> words;
    string word = "";
    while (cin >> word)
    {
        {
            if (word == "boo" || "broccoli" || "moist")
                word = "BLEEP";
        }
        words.push_back(word);
    }
    for (int i = 0; i < words.size(); ++i)
        cout << "" << words[i] << "";
}
if (word == "boo" || "broccoli" || "moist") 

不是将多个条件链接在一起的正确方法。你需要重新检查每个部位的状况。

if (word == "boo" || word == "broccoli" || word == "moist")

当你有

if (word == "boo" || "broccoli" || "moist") 

它被翻译成

if (word == "boo" || true || true)

所以这将永远是真的。