利用指定文件进行参考以进行单词的转换
2015-11-07 17:36
309 查看
#include <map>
#include <sstream>
#include <fstream>
#include <iostream>
#include <string>
#include <exception>
using namespace std;
ifstream& openfile(ifstream &in, const string &filename){
in.close();//close in case it was alrady open
in.clear();//clear any existing errors
in.open(filename.c_str());//open the file we were given
//in要么于指定文件绑定起来了,要么处于错误条件状态
return in;//condition state is good if open succeeded
}
int main(int argc, char** argv)
{
map<string, string> trans_map;
string key, value;
ifstream map_file;
if (!openfile(map_file, "transform.txt")){
throw runtime_error("no transformation file");
}
//read the tansformation map and build the map
while (map_file >> key >> value){
trans_map.insert(pair<string, string>(key, value));
}
ifstream input;
if (!openfile(input, "source.txt")){
throw runtime_error("no input file");
}
string line;//hold each line from the input
//read the text to transform it a line at a time
while (getline(input, line)){
istringstream stream(line); //read the line a word at a time
string word;
//读字符串流
bool lineFirst = true;//controls whether a space is printed
while (stream >> word){
//ok:the actual mapwork,this part is the heart of the program
map<string, string>::const_iterator iter = trans_map.find(word);
if (iter != trans_map.end()){
//replace it by the transformation value in the map
word = iter->second;
}
if (lineFirst){
cout << word;
lineFirst = false;
}
else{
cout << " " << word;//print space between words;
}
}
cout << endl;//done with this line of input
}
return 0;
}
#include <sstream>
#include <fstream>
#include <iostream>
#include <string>
#include <exception>
using namespace std;
ifstream& openfile(ifstream &in, const string &filename){
in.close();//close in case it was alrady open
in.clear();//clear any existing errors
in.open(filename.c_str());//open the file we were given
//in要么于指定文件绑定起来了,要么处于错误条件状态
return in;//condition state is good if open succeeded
}
int main(int argc, char** argv)
{
map<string, string> trans_map;
string key, value;
ifstream map_file;
if (!openfile(map_file, "transform.txt")){
throw runtime_error("no transformation file");
}
//read the tansformation map and build the map
while (map_file >> key >> value){
trans_map.insert(pair<string, string>(key, value));
}
ifstream input;
if (!openfile(input, "source.txt")){
throw runtime_error("no input file");
}
string line;//hold each line from the input
//read the text to transform it a line at a time
while (getline(input, line)){
istringstream stream(line); //read the line a word at a time
string word;
//读字符串流
bool lineFirst = true;//controls whether a space is printed
while (stream >> word){
//ok:the actual mapwork,this part is the heart of the program
map<string, string>::const_iterator iter = trans_map.find(word);
if (iter != trans_map.end()){
//replace it by the transformation value in the map
word = iter->second;
}
if (lineFirst){
cout << word;
lineFirst = false;
}
else{
cout << " " << word;//print space between words;
}
}
cout << endl;//done with this line of input
}
return 0;
}
相关文章推荐
- linux下安装opencv并生成opencv-java,即linux下用java调用opencv
- 安卓开发错误笔记
- 使用wireshark进行抓包
- Android Spinner
- 2014-2015 ACM-ICPC, Asia Tokyo Regional Contest A題:Bit String Reordering [bfs]
- PowerShell介绍 第十二回 函数
- 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(4)
- 烂泥:haproxy与nginx、zabbix集成
- python之函数用法vars()
- mysql学习笔记(2)
- zoj 1610 Count the Colors
- 【转】nat 穿透原理
- MVC4学习-View(0)
- 典型用户
- ——黑马程序员——OC加强部分 内存管理
- 短信验证 掌淘 mob 1.1.1开发实例
- Android项目中如何用好构建神器Gradle?
- linux下奇怪的“重名”文件
- 利用sql语句对数据库进行操作
- 把带横杠的字符串转为驼峰字符串