带参的main以文件名为参数完成单词转换功能 要用到的文件要放在与可执行程序相同的目录中 不然运行不了 代码: #include<iostream> #include<stdexcept> //导常处理头文件 #include<map> #include<string> #include<fstream> #include<sstream> using namespace std; ifstream &open_file(ifstream &in,const string &file) { in.close(); in.clear(); in.open(file.c_str()); return in; } int main(int argc,char **argv) { map<string,string> trans_map; string key,value;//cout <<argv[1] <<" " <<argv[2] <<endl; if(argc != 3) throw runtime_error("Wrong numbers of arguments!"); ifstream map_file; if(!open_file(map_file,argv[1])) throw runtime_error("Wrong numbers of arguments!"); while(map_file >> key >>value){ trans_map.insert(make_pair(key,value)); } ifstream input; if(!open_file(input,argv[2])) throw runtime_error("Wrong numbers of arguments!"); string line; bool firstword = true; while(getline(input,line)){ istringstream stream(line); string word; while(stream >>word){ map<string,string> :: iterator map_it = trans_map.find(word); if(map_it != trans_map.end()) word = map_it->second; if(firstword) firstword = false; else cout <<" "; cout <<word; } cout <<endl; } return 0; }