map——单词的转换
来源:互联网 发布:嘉艺云管理平台软件 编辑:程序博客网 时间:2024/04/30 09:09
程序实现目的:根据“转换规则文件(暗码)”对“待转换文件(明文)”进行转换。
使用数据结构:map的创建、搜索、以及遍历。
//map的创建、搜索、以及遍历//单词转换#include<iostream>//定义用于读写流的基本类型#include<map>#include<string>#include<fstream>//定义用于读写命名文件的类型#include<sstream>//定义用于读写内存string对象的类型using namespace std;//建立转换映射(map的创建)map<string, string> buildMap(ifstream &map_files){map<string, string> trans_map; //保存转换规则string key; //要转换的单词(缩略词)string value; //替换后的内容(缩略词相对应的完整内容)//读取第一个单词存入key,剩余的内容存入value。//在执行读取操作时,string对象会自动忽略开头的空白(空格、换行、制表符)并从第一个真正的字符开始读起,直到遇到下一处空白为止//getline函数的参数是一个输入流和一个string对象,函数从给定的输入流中读取内容,直到遇到换行符为止,然后把所读的内容存入那个string对象。while(map_files >> key && getline(map_files, value)){if (value.size() > 1)trans_map[key] = value.substr(1);//跳过缩略词和说完整内容之间空格————前导空格elsethrow runtime_error("no rule for" + key);}cout << "转换规则文件map:" << endl;for (auto s : trans_map)cout << s.first << " " << s.second << endl;cout << endl;return trans_map;}//生成转换文本(map的搜索)const string & word_transform(const string &s, const map<string, string> &m){auto map_it = m.find(s);//首先确定给定的string是否在map中if (map_it != m.cend())return map_it->second; //使用替换短语elsereturn s; //否则返回原string}//单词转换程序(map的遍历)void transform(ifstream &map_files,ifstream &input){auto trans_map = buildMap(map_files); //保存转化规则string text;cout << "转换后文本内容:" << endl;//转换后文本打印在控制台//用getline一行一行的读取输入文件是为了使输出中的换行位置能和输入文件中一样//使用istreamstring来处理当前行中的每个单词while (getline(input, text)) //读取输入文件的每一行{istringstream stream(text); //读取每个单词string word;bool firstword = true; //控制是否打印空格while (stream >> word){if (firstword)firstword = false;elsecout << " "; //在单词间打印一个空格cout << word_transform(word, trans_map);//打印输出}cout << endl; //完成一行的转换}////转换后文件输出在输出文件//cout << "转换后文件输出在输出文件(..\\out_files.txt)" << endl;//ofstream out("..\\out_files.txt");//while (getline(input, text)) //读取输入文件的每一行//{//istringstream stream(text); //读取每个单词//string word;//bool firstword = true; //控制是否打印空格//while (stream >> word)//{// if (firstword)//firstword = false;//else//out << " "; //在单词间打印一个空格//out << word_transform(word, trans_map); //输出//}//out << endl; //输出换行//}//out.close();}void main(){ifstream map_files, input_files;map_files.open("..\\map_files.txt");//转换规则文件放在该源程序的上一级目录下input_files.open("..\\input_files.txt");//待转换文件放在该源程序的上一级目录下//将待转换文件文本内容打印在控制台cout << "待转换文件文本内容:" << endl;string str;while (getline(input_files, str)){cout << str << endl;}cout << endl;//一旦一个文件流已经打开,它就保持与对应文件的关联。实际上,对于一个已经打开的文件流调用open会失败,随后的试图使用文件流的操作都会失败input_files.close();//关闭input_files,以便我们将其用于其他文件input_files.open("..\\input_files.txt");transform(map_files, input_files);}
0 0
- map——单词的转换
- 一个单词转换的map
- 单词转换程序——使用map&文件读写操作
- c++学习笔记——个单词转换的map程序详解
- 使用map实现单词转换的程序
- P391--一个单词转换的map
- 单词转换(stl map)
- 单词转换(map对象)
- c++ 单词转换 map对象
- c++学习笔记-map的使用实例(单词转换)
- C++中map容器实现单词转换的程序
- 使用map关联容器实现单词转换的程序
- 单词博弈——利用map
- 10.3.9例子“单词转换”map对象
- C++ primer 单词转换 map对象
- C++ 10.3.9 "单词转换"map对象
- C++ primer ————————————“单词转换" map 对象
- C++ Primer : 第十一章 : 关联容器示例: 一个单词转换的map
- 代码扫描事项和resin绑定多ip
- Ubuntu 用vsftpd 配置FTP服务器
- python学习之--调用shell脚本
- 我该补一补
- Functional MRI (second edition) -- 4. Basic Principles of MR Image Formation
- map——单词的转换
- QNX Neutrino工具版内核 [procnto-instr]
- An Overview of Oracle Business Intelligence Apps.
- 使用递归实现数的全排列
- 理解LinkedHashMap
- 极光推送---安卓Demo
- ios 生成二维码图片libqrencode
- TCP协议中的三次握手和四次挥手
- java绑定