#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; } void search(vector v) { int min = 0, max = v.size()-1, steps = 0; while (true) { int middle = (min+max)/2; steps += 1; string whichway; cout << steps << ": " << min << " " << middle << " " << max << " " << v[middle] << "\n? "; cin >> whichway; if (whichway == "before") max = middle - 1; else if (whichway == "after") min = middle + 1; else cout << "idiot!\n"; } } void main() { vector words = read_dictionary("/home/www/dics/pocket.txt"); cout << words[0] << "\n"; cout << words[words.size()/2] << "\n"; cout << words[words.size()-1] << "\n"; cout << "\nThink of a word, tell me 'before' or 'after' relative to the one I say.\n"; search(words); }