C++ 使用向量过滤一些预定义的字符串
因此,我目前正在做我的编程书《编程:使用c++的原则和实践》中的练习,这本书来自比亚恩·斯特劳斯特鲁普(Bjarne Stroustrup),我现在只能做一个练习。基本上,练习就是编写一个程序,大声说出它不喜欢的单词。它的工作方式是用户输入一个字符串,程序重复这个单词。如果用户输入的单词是厌恶向量的一部分,则该单词将替换为“Bleep”。(我不知道我是否解释对了,但理解起来应该不会太复杂) 这是我的程序版本:C++ 使用向量过滤一些预定义的字符串,c++,vector,C++,Vector,因此,我目前正在做我的编程书《编程:使用c++的原则和实践》中的练习,这本书来自比亚恩·斯特劳斯特鲁普(Bjarne Stroustrup),我现在只能做一个练习。基本上,练习就是编写一个程序,大声说出它不喜欢的单词。它的工作方式是用户输入一个字符串,程序重复这个单词。如果用户输入的单词是厌恶向量的一部分,则该单词将替换为“Bleep”。(我不知道我是否解释对了,但理解起来应该不会太复杂) 这是我的程序版本: int main() { string dislike = "Potato";
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;
}
intmain()
{
string=“马铃薯”;
字串=”;
(用词)
{
如果(单词==不喜欢)
{
coutintmain()
{
媒介厌恶;
不喜欢。推回(“土豆”);
不喜欢。推回(“花生”);
不喜欢。推回(“椰子”);
字符串;
(用词)
{
如果(查找(不喜欢.开始(),不喜欢.结束(),单词)!=不喜欢.结束())
{
好的,让我来解释一个简单的方法。有更优雅的方法,但现在重要的是,你要了解如何访问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!
}
}
第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;
}
}
for(vector::const_iterator it=haste.begin();it!=haste.end();++it){
if(*it==单词){
//我们发现一个阳性匹配,所以嘟嘟声然后离开这里
不能使用std::find(你的_vector.begin(),你的_vector.end(),单词)
intmain()
{
矢量厌恶;
不喜欢。推回(“土豆”);
不喜欢。推回(“花生”);
不喜欢。推回(“椰子”);
字串=”;
(用词)
{
if(std::find(不喜欢.begin(),不喜欢.end(),words)!=不喜欢.end())
{
这是我在读这本书时对书中那个问题的解答:)希望它是不言自明的
/*THE QUESTION GOES LIKE;
Write a program that “bleeps” out words that you don’t like; that is, you read in words
using cin and print them again on cout. If a word is among a few you have defined, you
write out BLEEP instead of that word. Start with one “disliked word” such as string
disliked = “Broccoli”;
When that works, add a few more.*/
#include "std_lib_facilities.h" // this is a standard library header that came with
the book
int main () {
vector<string> dislike = {"Dislike", "Alike", "Hello", "Water"}; /* defining a vector
for the disliked words. */
vector<string> words; //initializing vector for the read words.
cout << "Please enter some words\n"; //prompt user to enter some words.
for( string word; cin >> word;) //this current word typed is read in.
words.push_back(word); // word read in are pushed into the vector "words".
sort(words); /* function for the standard library for sorting data...this makes the data from the vector "words" appears in alphabetical order. */
for (int i=0; i<words.size(); ++i){ /*this acts as an iterator. and goes through all the element of the vector "words". */
if(i==0 || words[i-1]!=words[i]){ /*this prevents the words from repeating....just an option incase the user enters same kinda words twice or more. */
if(words[i]!=dislike[0] && words[i]!=dislike[1] && words[i]!=dislike[2] && words[i]!=dislike[3]) /*This test checks whether the words typed match any of the elements of the vector "dislike".if they don't match; */
cout << words[i]<< '\n'; //prints out the words.
else
cout << "BlEEP!\n"; //if they match....print out "BlEEP!".
}
}
}
/*问题是这样的;
写一个程序,用“哔哔”的一声说出你不喜欢的单词,也就是说,你在读单词
使用cin并在cout上再次打印。如果一个单词是您定义的几个单词中的一个,则
写出BLEEP而不是那个词。从一个“不喜欢的词”开始,比如string
不喜欢的花椰菜;
当这起作用时,再添加一些*/
#包括“std_lib_facilities.h”//这是随附的标准库标题
书
int main(){
向量厌恶={“厌恶”、“相似”、“你好”、“水”};/*定义向量
因为不喜欢的话*/
向量字;//初始化读取字的向量。
cout>word;)//已读入当前键入的单词。
words.push_back(word);//读入的单词被推入向量“words”中。
sort(words);/*函数用于对数据进行排序的标准库…这使矢量“words”中的数据按字母顺序显示*/
对于(int i=0;i我学习C++),这个程序已经改变了一些。
写一个程序,用“哔哔”的一声说出你不喜欢的脏话;也就是说,
您使用cin读入单词,然后在cout上再次打印。如果一个单词是您定义的几个单词中的一个,您可以写出BLEEP,或者让它发出BLEEP(声音),而不是该单词。从一个“坏单词”开始,例如--string badword=“arse”;如果可行,可以根据所有不希望打印出来的坏单词再添加一些或编写一个完整的程序
while (cin >> words)
{
if(find(badwords.begin(), badwords.end(),words) !=badwords.end())
{
cout << " " << endl; // You can put Bleep in or leave it out (Blank) if blank
// it will leave a blank in the phrase when it prints
Beep(523,500); // This is to Bleep (Sound) when a bad word is found
cin.get();
}
else
{
cout << words << endl;
}
}
while(cin>>单词)
{
如果(查找(badwords.begin(),badwords.end(),words)!=badwords.end())
{
我已经用前几章学到的思想解决了这个问题,没有超出你的理解范围
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> disliked;
//adding disliked words to the vector
disliked.push_back("dog");
disliked.push_back("cat");
disliked.push_back("goat");
disliked.push_back("cow");
disliked.push_back("sheep");
disliked.push_back("pig");
string words=""; //this variable will store the input from the user.
while(cin>>words)
{//test every entered word to see if it's equal to any word listed in disliked words.
if(words==disliked[0] ||//or
words==disliked[1] ||//or
words==disliked[2] ||//or
words==disliked[3] ||//or
words==disliked[4] ||//or
words==disliked[5]){
cout<<"Bleeps";}
else{
cout<<words;}
}
return 0;
//Not that I have not gone beyond what has been covered in the previous chapters.
//I know their are beautiful solutions to this problem.
//Keep learning you will know everything.
#包括
#包括
使用名称空间std;
int main()
{
不喜欢向量;
//向向量添加不喜欢的单词
不喜欢。推回(“狗”);
不喜欢。推回(“猫”);
不喜欢。推回(“山羊”);
不喜欢。推回(“母牛”);
不喜欢。推回(“羊”);
不喜欢。推回(“猪”);
string words=“;//此变量将存储用户的输入。
while(cin>>单词)
{//测试每个输入的单词,看它是否等于不喜欢的单词中列出的任何单词。
如果(单词==不喜欢[0]| |//或
words==不喜欢的[1]| |//或
单词==不喜欢的[2]| |//或
单词==不喜欢的[3]| |//或
单词==不喜欢的[4]| |//或
单词==不喜欢的[5]){
cout这个问题很久很久以前就被问到了,所以作者在这一点上可能是专业的,哈哈,但是对于寻找相同答案的人来说,这里有一个更简单但有效的解决方案。我从一开始就通过比亚恩的书学习,所以我还没有“受影响”有更高的知识让你困惑,但有足够好的解决方案,可以根据我们在书中所处的位置来工作。:)
//发出我们不喜欢的单词的程序
向量词;
向量bwords={“this”、“that”、“then”};//bleeped单词
字符串剑;//临时单词
cout>tword;)//用文字阅读
字。推回(第二);
//检查它们是否与哔哔声的单词匹配
cout//Josef.L
//2019/7/11
内部主(空){
向量输入;
用于(字符串pat;cin>>pat;){
输入。推回(pat);
}
对于(int i=0;i cout“简单可能比复杂更难:你必须努力让你的思想变得简单。但最终这是值得的,因为一旦你做到了,你就可以移山。”―史蒂夫·乔布斯
#include "std_lib_facilities.h"
int main()
{
vector<string>disliked;
disliked.push_back("Apple");
disliked.push_back("OliveOil");
disliked.push_back("Strawberry");
disliked.push_back("Lemon");
cout<<"Please type some words:"<<"\n";
string words=" ";
while(cin>>words)
{
if (words==disliked[0] | words==disliked[1]|
words==disliked[2] | words==disliked[3])
{cout<<"BLEEP"<<"\n";}
else{cout<<words<<"\n";}
}
keep_window_open();
}
#包括“std_lib_facilities.h”
int main()
{
不喜欢矢量;
不喜欢。推回(“苹果”);
不喜欢。推回(“橄榄油”);
不喜欢。推回(“草莓”);
不喜欢。推回(“柠檬”);
cout-1“请不要只给我”的哪一部分
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;
}
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)
{
if(std::find(dislike.begin(), dislike.end(), words) != dislike.end())
{
cout << "Bleep!" << endl;
}
else
{
cout << words << endl;
}
}
system("pause");
return 0;
}
/*THE QUESTION GOES LIKE;
Write a program that “bleeps” out words that you don’t like; that is, you read in words
using cin and print them again on cout. If a word is among a few you have defined, you
write out BLEEP instead of that word. Start with one “disliked word” such as string
disliked = “Broccoli”;
When that works, add a few more.*/
#include "std_lib_facilities.h" // this is a standard library header that came with
the book
int main () {
vector<string> dislike = {"Dislike", "Alike", "Hello", "Water"}; /* defining a vector
for the disliked words. */
vector<string> words; //initializing vector for the read words.
cout << "Please enter some words\n"; //prompt user to enter some words.
for( string word; cin >> word;) //this current word typed is read in.
words.push_back(word); // word read in are pushed into the vector "words".
sort(words); /* function for the standard library for sorting data...this makes the data from the vector "words" appears in alphabetical order. */
for (int i=0; i<words.size(); ++i){ /*this acts as an iterator. and goes through all the element of the vector "words". */
if(i==0 || words[i-1]!=words[i]){ /*this prevents the words from repeating....just an option incase the user enters same kinda words twice or more. */
if(words[i]!=dislike[0] && words[i]!=dislike[1] && words[i]!=dislike[2] && words[i]!=dislike[3]) /*This test checks whether the words typed match any of the elements of the vector "dislike".if they don't match; */
cout << words[i]<< '\n'; //prints out the words.
else
cout << "BlEEP!\n"; //if they match....print out "BlEEP!".
}
}
}
while (cin >> words)
{
if(find(badwords.begin(), badwords.end(),words) !=badwords.end())
{
cout << " " << endl; // You can put Bleep in or leave it out (Blank) if blank
// it will leave a blank in the phrase when it prints
Beep(523,500); // This is to Bleep (Sound) when a bad word is found
cin.get();
}
else
{
cout << words << endl;
}
}
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> disliked;
//adding disliked words to the vector
disliked.push_back("dog");
disliked.push_back("cat");
disliked.push_back("goat");
disliked.push_back("cow");
disliked.push_back("sheep");
disliked.push_back("pig");
string words=""; //this variable will store the input from the user.
while(cin>>words)
{//test every entered word to see if it's equal to any word listed in disliked words.
if(words==disliked[0] ||//or
words==disliked[1] ||//or
words==disliked[2] ||//or
words==disliked[3] ||//or
words==disliked[4] ||//or
words==disliked[5]){
cout<<"Bleeps";}
else{
cout<<words;}
}
return 0;
//Not that I have not gone beyond what has been covered in the previous chapters.
//I know their are beautiful solutions to this problem.
//Keep learning you will know everything.
// program that bleeps out words we dont like
vector <string> words;
vector <string> bwords = {"this", "that", "then"}; //bleeped words
string sword; // temporary word
cout << "Enter few words: ";
for (string tword; cin >> tword;) // read in words
words.push_back(tword);
//check if they match beeped words
cout << "\n\nWords:\n";
for (int i = 0; i < words.size(); i++) //take word[i] from the vector
{
sword = words[i]; // temporary variable is now word[i]
for (int j = 0; j < bwords.size(); j++) // take beeped word[j] from saved words
{
if (words[i] == bwords[j]) // is word[i] same as bleeped word[j]
sword = "BLEEP"; // if word[i] is same then replace sword with BEEP
}
cout << sword << "\n"; // now we checked first word and if it matches with any of the bleeped words then it will cout bleep, otherwise it will cout first word.
}
//Josef.L
//2019/7/11
int main(void){
vector <string> inpute;
for(string pat; cin >>pat;){
inpute.push_back(pat);
}
for(int i=0; i < inpute.size(); i++){
if("work"== inpute[i]){
cout<<"bleep! "<<endl;}
else if("life" == inpute[i]){
cout<<"bleep! "<<endl;
}
else if("broccoli" == inpute[i]){
cout<<"bleep! "<<endl;
}
else if("homework" == inpute[i]){
cout<<"bleep! "<<endl;
}
else{
cout <<inpute[i]<<endl;
}
}
return 0;}
//However, the entire source code is too long and boring, so there should be an improvement.
#include "std_lib_facilities.h"
int main()
{
vector<string>disliked;
disliked.push_back("Apple");
disliked.push_back("OliveOil");
disliked.push_back("Strawberry");
disliked.push_back("Lemon");
cout<<"Please type some words:"<<"\n";
string words=" ";
while(cin>>words)
{
if (words==disliked[0] | words==disliked[1]|
words==disliked[2] | words==disliked[3])
{cout<<"BLEEP"<<"\n";}
else{cout<<words<<"\n";}
}
keep_window_open();
}