jeudi 5 mai 2016

Parsing through Vectors

I am new and learning C++ using the Programming Principles ... book by Bjarne Stroustrup. I am working on one problem and can't figure out how to make my code work. I know the issue is with if (words[i]==bad[0, bad.size() - 1]) in particular bad.size() - 1])

I am trying to out put all words in the words vector except display a bleep instead of any words from the words vector that match any of the words in the bad vector. So I need to know if words[i] matches any of the values in the bad vector.

#include "../std_lib_facilities.h"

using namespace std;

int main()  
{
vector<string> words; //declare Vector
vector<string> bad = {"idiot", "stupid"};

//Read words into Vector
for(string temp; cin >> temp;)
        words.push_back(temp);
cout << "Number of words currently entered "
    << words.size() << '\n';

//sort the words
sort(words);

//read out words
for(int i = 0; i < words.size(); ++i)
    if (i==0 || words[i-1]!= words[i])
        if (words[i]==bad[0, bad.size() - 1])
        cout << "Bleep!\n";
        else
        cout << words[i] << '\n';

return 0;
}

Aucun commentaire:

Enregistrer un commentaire