/*
*author @alex
*title C++Primer 练习11.33 单词转换程序
*/
map<string, string> buildMap(ifstream &map_file) {
map<string, string> trans_map;
string key;
string value;
while (map_file >> key && getline(map_file, value)) {
if (value.size() > 1)
trans_map[key] = value.substr(1);
else
throw runtime_error("error");
}
return trans_map;
}
const string& transform(const string &s, const map<string, string>&m) {
auto ret = m.find(s);
if (ret != m.cend())
return ret->second;
else
return s;
}
void word_transform(ifstream &map_file, ifstream &input) {
auto trans_map = buildMap(map_file);
string text;
while (getline(input, text)) {
istringstream stream(text);
string word;
bool firstword = true;
while (stream >> word) {
if (firstword)
firstword = false;
else
cout << " ";
cout << transform(word, trans_map);
}
cout << endl;
}
}
int main(int argc, char **argv) {
ifstream map_file("a.txt");
ifstream aim_file("b.txt");
word_transform(map_file, aim_file);
}