map——单词的转换
2015-07-10 14:44
495 查看
程序实现目的:根据“转换规则文件(暗码)”对“待转换文件(明文)”进行转换。
使用数据结构: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);//跳过缩略词和说完整内容之间空格————前导空格
else
throw 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; //使用替换短语
else
return 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;
else
cout << " "; //在单词间打印一个空格
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);
}
使用数据结构: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);//跳过缩略词和说完整内容之间空格————前导空格
else
throw 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; //使用替换短语
else
return 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;
else
cout << " "; //在单词间打印一个空格
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);
}
相关文章推荐
- Python中map()函数浅析
- Android使用Google Map浅谈
- Erlang中的映射组Map详细介绍
- C++Primer笔记之关联容器的使用详解
- c++中map的基本用法和嵌套用法实例分析
- jquery中map函数与each函数的区别实例介绍
- jquery中map函数遍历数组用法实例
- jquery与google map api结合使用 控件,监听器
- jquery遍历数组与筛选数组的方法
- JavaScript中实现map功能代码分享
- 用json方式实现在 js 中建立一个map
- Android Map新用法:MapFragment应用介绍
- js Map List 遍历使用示例
- velocity显示List与Map的方法详细解析
- Google Map Api和GOOGLE Search Api整合实现代码
- 关于STL中的map容器的一些总结
- vector,map,list,queue的区别详细解析
- 浅析stl序列容器(map和set)的仿函数排序
- Java中List与Map初始化的一些写法分享
- Java用for循环Map详细解析