// stringconvert.cpp : 定义控制台应用程序的入口点。
//
#include <exception>
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <sstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
// string key,value;
// map<string,string> trans_map;
// //if (argc != 3)
// // throw runtime_error("Wrong number of arguments!");
//
// ifstream map_file("transfile.txt");
// //if (!map_file(argv[2])) {
// // throw runtime_error("No transformation file!");
// //}
// while (map_file >> key >> value ) {
// trans_map.insert(make_pair(key,value));
// }
// map_file.close();
//
// ifstream input("inputfile.txt");
// //if (!input(argv[2])) {
// // throw runtime_error("No input file!");
// //}
//
//
// string line;
// while (getline(input,line)) {
// istringstream stream(line);
// string word;
// bool firstWord = true;
// while (stream >> word) {
// map<string,string>::const_iterator mapIt = trans_map.find(word);
// if (mapIt != trans_map.end()) {
// word = mapIt->second;
// }
// if (firstWord) {
// firstWord = false;
// } else {
// cout<<ends;
// }
// cout<<word;
// }
// cout<<endl;
// }
map_file.close();
// input.close();
// return 0;
//注释部分为C++ primer标准答案
map<string,string> mapList;
string key,value;
ifstream trans_file("transfile.txt");
while (trans_file >> key >> value) {
mapList.insert(make_pair(key,value));
}
trans_file.clear();
trans_file.close();
ifstream input_file("inputfile.txt");
string input;
while (getline(input_file,input)) {
string word;
istringstream inputWord(input);
while (inputWord >> word) {
map<string,string>::const_iterator mapIter = mapList.find(word);
if (mapIter != mapList.end()) {
word = mapIter->second;
}
cout<<word<<ends;
}
cout<<endl;
}
input_file.clear();
input_file.close();
return 0;
}
明密对照表:transfile.txt
'em them
cuz because
gratz grateful
i I
nah no
pos supposed
sez said
tanx thanks
wuz was
加密后的文件inputfile.txt
nah i sez tanx cuz i wuz pos to
not cuz i wuz gratz