#include #include "map.h" map::map(){ myNumberWords = 0; } map::map(int size): left(size), right(size){ myNumberWords = 0; } string map::getReplacement(const int& position) const{ if(position >= myNumberWords){ cerr << "Illegal position." << endl ; exit(EXIT_FAILURE) ; }//if return right[position] ; } void map::insert(const string & word1, const string & word2){ int position; if(!search(word1, position)){ int size = left.length(); if(size == myNumberWords){ left.resize(2*size+1); right.resize(2*size+1); } //for(int i = position; i < myNumberWords; i++){ for(int i = myNumberWords-1 ; i >= position ; i--){ left[i+1] = left[i]; right[i+1] = right[i]; } left[position] = word1; right[position] = word2; myNumberWords++; } } bool map::search(const string & word){ int position; return search(word, position); } bool map::search(const string & word, int & position){ int first = 0; int last = myNumberWords - 1; unsigned int mid; // Search while there is more of the Vector to look at while (first <= last) { // Look at the midpoint of the Vector mid = (first + last) / 2; // word is found if (word == left[mid]) { position = mid; return true; } // word is in the second half else if (word > left[mid]) first = mid + 1; // word is in the first half else if (word < left[mid]) last = mid - 1; } position = first; return false; } void map::print(){ for(int i = 0; i < myNumberWords; i++) cout << left[i] << " " << right[i] << endl; cout << endl; }