2017-12-12 4 views
0

ベクトル禁止は、私があまりにも禁止リストに使用std::find彼らは同じ文字列であれば大きなベクトルに対してこの打ち切りプログラムをコーディングする方法は?

int main() 

{ 

    vector<string>banned = {"warez","blood"};  //words to be censored 
    vector<string>words; 

for(string t;cin >>t;) words.push_back(t); //creating database 
cout<<'\n'; 

for(string i : words) cout<<i<<'\t';  //non censored output of 
                database 

cout<<'\n'; 

for(int i = 0;i<words.size();++i)   //censored output of database 

if(banned[0] == words[i] || banned[1] == words[i]) 
    cout<<"Bleep"<<'\t'; 

else cout<<words[i]<<'\t'; 

    } 
+1

コードを整理できますか? –

+0

'std :: vector'の代わりに' std :: set'を使用していますか?とにかく 'std :: set'で' banned'の値をコピーして、より簡単にチェックすることができます。 – max66

+0

そして、なぜそれは正確にできないのですか? – Ivan

答えて

0

を参照してくださいベクトルワードから禁止ベクトルを比較するためのループを生成することはできませんよ多くの値を持つ場合。 ref:http://en.cppreference.com/w/cpp/algorithm/find

#include<algorithm> 
... 
if(std::find(banned.begin(), banned.end(), words[i]) != banned.end()) 
    std::cout << "Bleep" << '\t'; 
+0

私はループ文字列が等しいかどうかをチェックし、使用方法|| (または)演算子を私の恩恵を受ける –

+0

ifの中でループすることはできませんが、何かを行い、 'bool'を返す関数を呼び出すことができます。あなたのケースでは、 'std :: find'は、あなたが望むようにループ内で等価性をチェックします。したがって、ループ内に独自の関数を書く必要はありません。 – balki

関連する問題