C++ Primer 第十章 10.3.9 “单词转换”map对象
来源:互联网 发布:淘宝客网站app制作 编辑:程序博客网 时间:2024/06/06 14:28
如果单词转换文本的内容是:
'em them
cuz because
gratz grateful
i I
nah no
pos supposed
sez said
tanx thanks
wuz was
而要转换的文本是:
nah i sez tanx cuz i wuz pos to
not cuz i wuz gratz
那么:程序将产生如下输出结果:
no I said thanks because I was supposed to
not because I was grateful
源代码
#include<iostream>
#include<fstream>
#include<map>
#include<string>
#include<sstream>
#include<vector>
#include<exception>
#include<stdexcept>
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;
if(argc!=3)
{
throw runtime_error("wrong number of arguments");
}
ifstream map_file;
if(!open_file(map_file,argv[1]))
{
throw runtime_error("no transformation file");
}
while(map_file>>key>>value)
{
trans_map.insert(make_pair(key,value));
}
ifstream input;
if(!open_file(input,argv[2]))
{
throw runtime_error("no input file");
}
string line;
while(getline(input,line))
{
istringstream stream(line);//read a word at a time
string word;
bool firsetword=true;//control whether a space is printed
while(stream>>word)
{
map<string, string>::const_iterator map_it=trans_map.find(word) ;
if (map_it!=trans_map.end())
{
/* code */
word=map_it->second;
}
if (firsetword)
{
/* code */
firsetword=false;
}
else
{
cout<<" ";
}
cout<<word;
}
cout<<endl;
}
return 0;
}
1/
g++ -o hehe tran_map.cc
2/
./hehe "hello1.txt" "wanttoreplace.txt"
3/
hello1.txt
asd su
fgh min
jkl zhang
qwe xiao
rty qiu
uio hong
i I
4/
wanttoraplace.txt
asd fgh jkl love qwe rty uio
5/
输出
su min zhang love xiao qiu hong
- C++ Primer 第十章 10.3.9 “单词转换”map对象
- C++primer 第十章 单词转换程序 运行不了解决办法
- C++ primer 单词转换 map对象
- 10.3.9例子“单词转换”map对象
- C++ 10.3.9 "单词转换"map对象
- C++ primer中,"单词转换"map对象源码问题
- C++ primer 单词转换(使用map对象)
- c++primer关联容器中的“单词转换map程序”分析
- C++Primer第11章 一个单词转换的map【程序】
- 单词转换(map对象)
- c++ 单词转换 map对象
- C primer plus 第十章 练习9:
- 【C++primer】第十章——关联容器——单词统计程序
- C++ Primer : 第十一章 : 关联容器示例: 一个单词转换的map
- C++ primer ————————————“单词转换" map 对象
- 对C++ Primer的10.3.9单词转换的思考
- C++Primer第十章TextQuery
- c primer plus 第十章
- [北京] ThoughtWorks 北京招聘 C#开发工程师(一年左右经验即可)
- Spark集群安装
- QTP错误处理机制
- SQLite
- 基于Android中dp和px之间进行转换的实现代码
- C++ Primer 第十章 10.3.9 “单词转换”map对象
- Error writing to data sink! Some output may get lost
- plupload上传例子
- 数据挖掘工程师笔试及答案整理
- 栈操作之顺序栈
- ScrollView子布局填满整个屏幕
- iOS开发 git github 初学+进阶
- 麦子学院美国商务中心成立,探索在线教育新高点www.maiziedu.com
- 自定义绘画歌词,产生滚动效果