#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <map>
using namespace::std;
int main()
{
string f1("D://1.txt"),f2("D://2.txt"),f3("D://3.txt");
map<string,string> map_trans;
string key,value;
ifstream trans_file(f1.c_str());
if(!trans_file)
cerr<<"No translation file!"<<endl;
while(trans_file>>key>>value)
map_trans.insert(make_pair(key,value));
// Read the translation file and build map
ifstream input_file(f2.c_str());
if(!input_file)
cerr<<"No input file!"<<endl;
ofstream output_file(f3.c_str());
if(!output_file)
cerr<<"No Output file!"<<endl;
string line;
while(getline(input_file,line))
{
istringstream stream(line);
string word;
bool firstword=true;
while(stream>>word)
{
map<string,string>::const_iterator map_it=map_trans.find(word);
if(map_it!=map_trans.end())
word=map_it->second;
if(firstword)
firstword=false;
else output_file<<" ";
output_file<<word;
}
output_file<<endl;
}
system("pause");
return 0;
}
1.txt 內容初始化
'em them cuz because grantz grateful i I nah no pos supposed sez said tanx thanks wuz was
2.txt 內容初始化
nah i sez tanx cuz i wuz pos to not cuz i wuz grantz
3.txt 裡面將輸出
no I said thanks because I was supposed to not because I was grateful