#include #include #include #include #include #include using namespace std; map BuildMap (ifstream &mapFile) { map transMap; //save the rule of transform string key; //the word will be transformed string value; //after-be-replaced word while (mapFile >> key && getline (mapFile, value)) { if (value.size () > 1) //if trans form role is exist transMap[key] = value.substr (1); //skip the first space else throw runtime_error ("no rule for" + key); } return transMap; } const string& transform (const string &s, map &tempMap) { return (tempMap.find (s) != tempMap.cend ()) ? tempMap[s] : s; } void WordTransform (ifstream &mapFile, ifstream &input) { auto transMap = BuildMap (mapFile); //save the rule of transform string text; //save everyline of the input file while (getline (input, text)) { //read every line istringstream stream (text); //read every word string word; bool firstWord = true; while (stream >> word) { //read text word by word if (firstWord) firstWord = false; else cout << " "; //space between every word //function transform return the first argument or the format have be transformed cout << transform (word, transMap); } cout << endl; //finish the transform of a line } } int main (int argc, char* argv[]) { std::ios::sync_with_stdio (false); ifstream mapFile (argv[1]), input (argv[2]); WordTransform (mapFile, input); return 0; }