使用向量对某些预定义字符串进行哔哔声处理
Using vectors to bleep some predefined strings
所以我目前正在做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;
}
正如你所看到的,这个版本没有使用向量(它应该使用向量,因为exercise正好在本章中对向量的解释之后(。所以我的问题是,我如何实现一个向量,其中有很多"不喜欢"的词,比如
vector<string>dislike;
dislike.push_back("Potatoes");
dislike.push_back("Peanuts");
dislike.push_back("Coconut");
让它像我的另一个版本一样工作,没有矢量(重复单词,但用哔哔声说出不喜欢的单词(。我似乎不明白如何在矢量中导航,以至于它只会发出"不喜欢"的声音。
如果有人能帮我解释一下它是如何工作的(请不要只给我答案(,我将不胜感激。
感谢您的时间和帮助,单独学习c++并不总是简单的,我感谢这个网站让我的学习曲线变得更容易了。
bobicol
好的,让我解释一个简单的方法。有更优雅的方法,但现在重要的是,你要了解如何访问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!
}
}
通过调用begin()
,可以获得向量中第一个元素的迭代器,然后继续递增(++it
(,直到到达向量的末尾。我在这里使用const_iterator
,因为我不打算修改任何元素,如果需要,请使用iterator
。
对于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
}
}
第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;
}
}
第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;
}
第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;
}
查看std::find
,了解更惯用的解决方案——它基本上为您省去了内部循环。如果您重新构造一点,您还可以在最后一个代码示例中去掉bool
。我将把它作为一个练习留给您(提示:保持迭代器的活动状态,并在终止循环后检查其值(。
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(find(dislike.begin(), dislike.end(), words) != dislike.end())
{
cout << "Bleep!" << endl;
}
else
{
cout << words << endl;
}
}
system("pause");
return 0;
}
对于std::find
,将#include <algorithm>
添加到源中。
使用std::find(your_vector.egin((,your_vector.end((,words(
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 wordsn"; //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!".
}
}
}
我正在学习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;
}
}
自从有人给出答案后,我对程序进行了一些更改。这是你要学习的。这在Visual Studio Express 2012 上运行
我已经使用前几章中已经学到的思想解决了这个问题,而不是超出您的理解范围。
#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.
}
这个问题很久很久以前就被问到了,所以作者在这一点上可能是专业的,哈哈,但对于任何寻求相同答案的人来说,这里有一个更简单但有效的解决方案。我从一开始就通过Bjarne的书学习,所以我还没有被更高的知识所"影响",让你感到困惑,而是根据我们在书中的进展,找到了足够好的解决方案。:(
// 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 << "nnWords: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.
}
现在,在这个例子中,你可以添加许多新的哔哔声单词,而不需要更改代码。这不是"现实生活"编程中的最佳解决方案,但在这本书中,我们学习了向量(不是很多(、cout、cin。。等等,所以其他任何事情看起来都很困惑。。在这之前,我们还不知道使用:,begin,true/fals,cin.get或类似的东西。
//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> dislike;
dislike.push_back("Potatoes");
dislike.push_back("Peanuts");
dislike.push_back("Coconut");
vector<string> words;
for (string temp_word; cin >> temp_word; )
{
for (int i = 0; i < dislike.size(); ++i)
{
if (temp_word == dislike[i])
{
words.push_back("BLEEP");
break;
}
else if (i == dislike.size() - 1 && temp_word != dislike[dislike.size() - 1])
{
words.push_back(temp_word);
break;
}
}
}
for (string temp_word : words)
{
cout << temp_word << ' ';
}
keep_window_open();
}
"简单可能比复杂更难:你必须努力工作,让你的思维变得干净,才能让它变得简单。但最终这是值得的,因为一旦你做到了,你就可以移山倒海。"―Steve Jobs
#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();
}
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 是否可以在c++中处理字符串流中的各个元素
- 如何以这种方式处理字符串数组
- 在C /Fortran Interop中处理字符串的官方方法是什么?
- 如何在处理C++字符串时使用内存集
- 当我们使用字符串而不是字符数组时如何处理字符串的字符
- 在处理 C 字符串时,如何将用户的第一个输入替换为当前输入
- 为什么 string.length()=0 在使用 string[i] 处理字符串后
- Qt究竟是如何处理字符串的,或者如何生成填充数字
- 处理字符串时出现超级奇怪的错误
- STXXL处理字符串
- FSNF 无法正确处理字符串
- 未定义的引用C++队列模板处理字符串
- 与Arduino处理字符串的方式混淆
- 如何让 atoi() 函数处理字符串
- 我似乎无法让 v.push.back() 处理字符串
- 使用指针处理字符串
- 使用MMX指令处理字符串
- strcmp()可以在c++中处理字符串吗?
- 试图像字符数组一样填充一个字符串,但它只是处理字符串中的第一个字符