如果禁止的矢量有很多值,我无法生成一个循环来比较从矢量字中禁止的矢量,看看它们是否是相同的字符串
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';
}
答案 0 :(得分:0)
在禁止列表中使用std::find
。参考: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';