初学者:虽然循环不能正常工作

时间:2015-06-06 00:41:53

标签: c++ loops vector while-loop

我还是初学者,我正在读书。有一个演习要求我根据过滤词的向量过滤输入,如果是其中之一,则输出"坏词"

这是与书中完全相同的练习。

  

试试这个   写一个程序,“ble”你不喜欢的单词;也就是说,你用cin阅读单词并在cout上再次打印它们。如果一个单词是你定义的几个单词之一,你就会写出BLEEP而不是那个单词。从一个“不喜欢的词”开始,如字符串disliked =“Broccoli”   当它工作时,再添加一些。;

这是我写的代码:

#include <D:\std_lib_facilities.h>

int main()
{

    // RL: omitting actual "bad" words to protect the innocent...
    vector <string> bwords { "word1", "word2", "word3" };

    vector <string> words;
    string input = "";

    while(cin >> input)
    {
        words.push_back(input);
    }

    double counter1 = 0;
    double counter2 = 0;

    while(counter1 < bwords.size() && counter2 < words.size())
    {
        if(bwords[counter1] == words[counter2])
        {
            cout << " bad word ";
        }
        else if (counter1 == bwords.size() - 1 && counter2 != words.size() )
        {
            cout << " "<< words[counter2] <<" ";
            counter1 = 0;
        }
        else
        {
            ++counter1;
            counter2 += 1 / bwords.size();
        }
    }
}

每当它开始时它只是测试第一个单词并重复它自己,好像只是测试第一个if条件。

1 个答案:

答案 0 :(得分:2)

你过度复杂了你的循环。尝试更像这样的东西:

#include <vector>
#include <string>
#include <algorithm>

using namespace std;

// RL: omitting actual "bad" words to protect the innocent...
const vector <string> bwords { "word1", "word2", "word3" };

string bleepWordIfBad(const string &word)
{
    if (std::find(bwords.begin(), bwords.end(), word) != bwords.end())
        return "BLEEP";
    else
        return word;
}

int main()
{    
    vector <string> words;
    string input;

    while (cin >> input)
        words.push_back(input);

    for (int counter = 0; counter < words.size(); ++counter)
        cout << " " << bleepWordIfBad(words[counter]) << " ";

    /*
    Alternatively:

    for (vector<string>::iterator iter = words.begin(); iter != words.end(); ++iter)
        cout << " " << bleepWordIfBad(*iter) << " ";
    */

    /*
    Alternatively:

    for (const string &word : words)
        cout << " " << bleepWordIfBad(word) << " ";
    */

    return 0;
}

或者,完全摆脱手动循环:

#include <vector>
#include <string>
#include <algorithm>

using namespace std;

// RL: omitting actual "bad" words to protect the innocent...
const vector <string> bwords { "word1", "word2", "word3" };

string bleepWordIfBad(const string &word)
{
    if (std::find(bwords.begin(), bwords.end(), word) != bwords.end())
        return "BLEEP";
    else
        return word;
}

void outputWord(const string &word)
{
    cout << " " << bleepWordIfBad(word) << " ";
}

int main()
{    
    vector <string> words;
    string input;

    while (cin >> input)
        words.push_back(input);

    for_each(words.begin(), words.end(), outputWord);

    /*
    Alternatively:

    for_each(words.begin(), words.end(),
        [](const string &word) { cout << " " << bleepWordIfBad(word) << " "; }
    );
    */

    return 0;
}

或者,完全删除输入vector,只是在输入时过滤用户的输入:

#include <vector>
#include <string>
#include <algorithm>

using namespace std;

// RL: omitting actual "bad" words to protect the innocent...
const vector <string> bwords { "word1", "word2", "word3" };

string bleepWordIfBad(const string &word)
{
    if (std::find(bwords.begin(), bwords.end(), word) != bwords.end())
        return "BLEEP";
    else
        return word;
}

int main()
{    
    string word;

    while (cin >> word)
        cout << " " << bleepWordIfBad(word) << " ";

    return 0;
}