Unter Verwendung der Binären Suche mit Vektoren.
Ich versuche, die Implementierung eines Algorithmus, der für jede Zeichenfolge in der ersten vector-es führt eine binäre Suche in den zweiten Vektor und output "JA:" wenn es eine übereinstimmung findet, oder "No:" sonst.
Moment mit meinem Programm mein algo immer die Ausgabe "NEIN:" und ich kann nicht herausfinden, was falsch läuft. Irgendwelche Hinweise oder Tipps wäre dankbar.
Meine Binäre Suche:
bool binary_search(const vector<string>& sorted_vec, string key) {
size_t mid, left = 0 ;
size_t right = sorted_vec.size(); //one position passed the right end
while (left < right) {
mid = left + (right - left)/2;
if (key > sorted_vec[mid]){
left = mid+1;
} else if (key < sorted_vec[mid]){
right = mid;
} else {
return true;
}
return false;
}
}
Mein Algo:
if(algo_speed == "fast"){
string key = fileContent[i];
while(getline(ifs1, line)){
fileContent1.push_back(line);
}
sort(fileContent1.begin(), fileContent1.end());
for(size_t i = 0; i < fileContent.size(); i++){
string temp = fileContent[i];
bool found = binary_search(fileContent1,temp) ;
if(found == true) {
cout << "YES:" << fileContent.at(i) << endl;
} else {
cout << "NO:" << fileContent.at(i) << endl;
}
}
}
InformationsquelleAutor user2757849 | 2013-09-12
Du musst angemeldet sein, um einen Kommentar abzugeben.
Sind Sie verlassen Ihre Funktion auf den ersten zu verpassen fehl am Platze
return false
:InformationsquelleAutor Nemanja Boric