私はいくつかのSTLアルゴリズムで遊んでいます。binary_search を使用している間、行き詰まりました。ベクター辞書をソートしてから、独自のコンパレーター関数を作成して binary_search を実行しています。ただし、出力されるたびに「見つかりません」。ただし、検索する文字列はベクトルにあります。どんな助けでも大歓迎です。
スニペットは次のとおりです。
bool ownComparator(const string &a, const string &b){
return lexicographical_compare(a.begin(),a.end(),b.begin(),b.end());
}
...
...
cout<<"Now using Binary Search to search in sorted array"<<endl;
string searchStr="will";
bool b = binary_search(dictionary.begin(),dictionary.end(),searchStr, ownComparator);
if(b) cout<<"Found";
else cout<<"Not Found";