使用map关联容器实现单词转换的程序
来源:互联网 发布:股票t 0交易平台源码 编辑:程序博客网 时间:2024/05/07 19:40
针对C++Primer(第四版)10.3.9节的单词转换程序实现与分析,参照书本p317-318。
程序代码如下:
#include <iostream>#include <sstream>//使用istringstream、ostringstream、stringstream;//三个类创建的对象必须包含的头文件;#include <string>#include <fstream>#include <map>using namespace std;int main(){map<string,string> trans_map;//创建一个空的map关联容器;string key,val;ifstream map_file;//以读方式打开转换文件;map_file.open("map.dat",ios::in);if(!map_file){cerr<<"Unable to open file : map.dat"<<endl;return -1;}while (map_file>>key>>val)//将映射文件的单词对放在map关联容器中;{trans_map.insert(make_pair(key,val));}ifstream input;//以读方式打开输入文件input.open("inputfile.dat",ios::in);string line;while (getline(input,line))//获取一行字符串{istringstream stream(line);//对象用来把一个已定字符串中的以空格隔开的内容提取出来string word;bool firstword =true;while (stream >> word){map<string,string>::const_iterator map_it=trans_map.find(word);if (map_it != trans_map.end())word = map_it->second;if(firstword)firstword=false;elsecout<<" ";cout<<word;}cout<<endl;}return 0;}
map.dat文件内容为:
’em them
cuz because
gratz grateful
i I
nah no
pos supposed
sez said
tanx thanks
wuz was
inputfile.dat文件内容为:
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
0 0
- 使用map关联容器实现单词转换的程序
- 使用map实现单词转换的程序
- C++中map容器实现单词转换的程序
- c++primer关联容器中的“单词转换map程序”分析
- C++ Primer 关联容器 单词转换程序
- C++ Primer : 第十一章 : 关联容器示例: 一个单词转换的map
- 单词转换程序-练习关联型容器
- 使用map关联容器
- 关联容器------map与pair的使用
- 单词转换程序——使用map&文件读写操作
- 使用Java容器的Map接口统计单词个数
- 用关联容器实现文本替换单词
- 实现单词转换的小程序
- 关联容器map应用-统计单词出现频率
- STL之关联容器(pair、map、set的使用)
- STL之关联容器(pair、map、set的使用)
- 关联容器-map与pair的使用(续)
- 经典的使用关联数组的单词计数程序
- 可定位性设计
- 使用遗传算法解决多变量函数优化问题!
- 态度决定命运,将自己的梦想抛向天空,你就能飞翔
- Spring mvc 接受请求参数的方式
- android——短信的发送
- 使用map关联容器实现单词转换的程序
- 无边框窗体的拖动
- android:addStatesFromChildren="true"
- FreeBSD + Win7 挂载Windows文件夹报 mount_smbfs unable to open connection: syserr = Authentication 的解决方法
- unix/linux常用网络命令系列:前言
- 对话框控件自动调整
- STL 排序比较(六)
- poj3256(SPFA最短路)
- Lucene中的highlighter