问题:判断w1是否在wordList中
string w1;
vector<string> wordList;
方法一:利用count方法
count() 统计vector中指定元素的数量
if(!count(wordList.begin(), wordList.end(), w1)) {
// count(wordList.begin(), wordList.end(), w1) == 0;
cout <<"w1不在wordList中" << endl;
}
else{
cout <<"w1在wordList中" << endl;
}
方法二:利用迭代器和find方法
auto it = find(wordList.begin(), wordList.end(), w1);
if(it == wordList.end()){
cout << "w1不在wordList中" << endl;
}
else{
cout << "w1在wordList中" << endl;
}