将一个向量的每个元素与另一个向量的元素进行比较
comparing each element of one vector to another's
所以我是一个新手c++学习者。我刚刚读完了 "Principles and Practice using C++"(第 2 版)的前 4 章。一本书中有一个问题,基本上是要求我阅读一个句子,而不是过滤掉 "bleeps" 我不喜欢的单词。所以我的想法是,首先我将我不喜欢看到的任何单词读入一个向量,然后我在另一个向量中读入一个句子左右以便稍后打印出来。然后我尝试将"print out"向量的每个元素与"disliked"向量进行比较,如果它们相同,我将其重写为"beep"。但是我不知道如何编写代码。谁能帮我?如果我的想法是错误的,有没有更简单的方法来做到这一点?谢谢
#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS
#include "../../../std_lib_facilities.h"
int main()
{
vector<string> disliked;
cout << "Enter the disliked words: ";
for (string dword; cin >> dword;)
disliked.push_back(dword);//inserting words to vector that's used to
//compare with
vector<string> words;
cout << "Enter words: \n";
for (string word; cin >> word;)
words.push_back(word);
cout << "Number of words: " << words.size() << '\n';//inserting words to
//vector in order to print out
for (int x = 0, y = 0; x < words.size() , y < disliked.size(); x++, y++)
if (words[x] = disliked[y])//this part is where it says it's wrong
words[x] = "beep";
sort(words.begin(),words.end());
for (int i = 0; i < words.size(); i++)
if (i == 0 || words[i - 1] != words[i])
cout << words[i]<<'\n'; //not show repeated words
程序在读取不喜欢的词的for循环后停止,因为for循环"cin>>word"中的条件实际上是不够的,它会接受你输入的任何字符或字符串,所以所有的词您正在进入的是被推入不喜欢的向量本身。
因此将条件更改为类似的内容,当用户输入字符串 "END" 或其他内容时停止 for 循环。
for (string dword; cin >> dword && dword!="END";)
disliked.push_back(dword);
而且下面的部分代码是错误的,
for (int x = 0, y = 0; x < words.size() , y < disliked.size(); x++, y++)
{
if (words[x] = disliked[y])//this part is where it says it's wrong
words[x] = "beep";
}
您需要检查每串不喜欢的向量到每串词向量。比较应该是这样的。
for (int x = 0; x < words.size() ; x++)
{
for(int y=0;y<disliked.size();y++)
{
if (words[x] == disliked[y])
words[x] = "beep";
}
}
所以我是一个新手c++学习者。我刚刚读完了 "Principles and Practice using C++"(第 2 版)的前 4 章。一本书中有一个问题,基本上是要求我阅读一个句子,而不是过滤掉 "bleeps" 我不喜欢的单词。所以我的想法是,首先我将我不喜欢看到的任何单词读入一个向量,然后我在另一个向量中读入一个句子左右以便稍后打印出来。然后我尝试将"print out"向量的每个元素与"disliked"向量进行比较,如果它们相同,我将其重写为"beep"。但是我不知道如何编写代码。谁能帮我?如果我的想法是错误的,有没有更简单的方法来做到这一点?谢谢
#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS
#include "../../../std_lib_facilities.h"
int main()
{
vector<string> disliked;
cout << "Enter the disliked words: ";
for (string dword; cin >> dword;)
disliked.push_back(dword);//inserting words to vector that's used to
//compare with
vector<string> words;
cout << "Enter words: \n";
for (string word; cin >> word;)
words.push_back(word);
cout << "Number of words: " << words.size() << '\n';//inserting words to
//vector in order to print out
for (int x = 0, y = 0; x < words.size() , y < disliked.size(); x++, y++)
if (words[x] = disliked[y])//this part is where it says it's wrong
words[x] = "beep";
sort(words.begin(),words.end());
for (int i = 0; i < words.size(); i++)
if (i == 0 || words[i - 1] != words[i])
cout << words[i]<<'\n'; //not show repeated words
程序在读取不喜欢的词的for循环后停止,因为for循环"cin>>word"中的条件实际上是不够的,它会接受你输入的任何字符或字符串,所以所有的词您正在进入的是被推入不喜欢的向量本身。
因此将条件更改为类似的内容,当用户输入字符串 "END" 或其他内容时停止 for 循环。
for (string dword; cin >> dword && dword!="END";)
disliked.push_back(dword);
而且下面的部分代码是错误的,
for (int x = 0, y = 0; x < words.size() , y < disliked.size(); x++, y++)
{
if (words[x] = disliked[y])//this part is where it says it's wrong
words[x] = "beep";
}
您需要检查每串不喜欢的向量到每串词向量。比较应该是这样的。
for (int x = 0; x < words.size() ; x++)
{
for(int y=0;y<disliked.size();y++)
{
if (words[x] == disliked[y])
words[x] = "beep";
}
}