使用循环重新分配向量中的字符串
reassigning strings in a vector using loops
我是一个学习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)
所以这将永远是真的。
相关文章:
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 构造<int>具有 2 个字符串文字的向量
- 如何将元素添加到向量的字符串位置
- 将向量解析<string>为字符串
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- 在 C++11 中,如何查找并返回以给定字符串开头的字符串向量中的所有项?
- 如何使用向量字符串令牌构建表达树
- 将 argv 复制到新的全局向量字符串
- 如何读取控制台输入并将其存储到向量字符串中
- 向量字符串push_back在C 中不起作用
- C char数组和向量字符串
- 可以直接初始化向量<字符串 *> 而不使用 new 吗?
- 迭代向量字符串的字符(C++禁止指针和整数之间的比较)
- 比较两个向量字符串的相似性C++
- 我有麻烦将字符串放入向量字符串
- 如何从一个向量字符串得到一个字符串的联合集
- 函数返回向量/字符串,但不返回数组
- 向量向量字符串固定大小
- c++向量字符串排序不工作
- C++将字符串数组[]复制到向量<字符串>