2017-12-12 10 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'에서'금지 '된 값을 복사하여 검사를 더 쉽게 할 수 있습니다. – 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