std::map<string, string> buildMap(ifstream &map_file)
{
std::map<string, string> trans_map;
for (string key, value; map_file >> key && getline(map_file, value); )
if (value.size() > 1) trans_map[key] = value.substr(1).substr(0, value.find_last_not_of(“” “”));
return trans_map;
}
const string & transform(const string &s, const std::map<string, string> &m)
{
auto map_it = m.find(s);
return map_it == m.cend() ? s : map_it->second;
}
void word_transform(ifstream &map, ifstream &input)
{
auto trans_map = buildMap(map);
for (string text; getline(input, text); ) {
std::istringstream iss(text);
for (string word; iss >> word; )
std::cout << transform(word, trans_map) <<endl; //此处必须加上endl,可能是因为版本问题或是电脑问题
std::cout << std::endl;
}
}