#include #include #include #include vector read_dictionary(string filename) { vector d; ifstream fin(filename.c_str()); while (true) { string s; fin >> s; if (fin.fail()) break; d.push_back(s); } fin.close(); cout << d.size() << " words read\n"; return d; } int search(vector v, string wanted) { int min = 0, max = v.size()-1; while (min <= max) { int middle = (min+max)/2; if (wanted < v[middle]) max = middle - 1; else if (wanted > v[middle]) min = middle + 1; else return middle; } return -1; } void main() { vector words = read_dictionary("/home/www/dics/pocket.txt"); string s; cout << "look for: "; cin >> s; int pos = search(words, s); if (pos < 0) cout << "Not found\n"; else cout << "Found at index " << pos << ": " << words[pos] << "\n"; }