码迷,mamicode.com
首页 > 编程语言 > 详细

c++学习笔记——个单词转换的map程序详解

时间:2015-01-13 22:56:45      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:

实现功能:给定一个string,将它转换为另一个string。程序输入是两个文件,第一个文件保存转换规则,第二个文件为将要进行转换的文本。

IDE:Windows7+VS2013

 

[cpp] view plaincopy
 
  1. #include "stdafx.h"  
  2. #include <map>  
  3. #include <iostream>  
  4. #include <fstream>  
  5. #include <string>  
  6. #include <stdexcept>  
  7. #include <sstream>  
  8. using namespace std;  
  9.   
  10. map<string, string> buildMap(ifstream &map_file)     //读入给定rules.text文件,建立转换映射  
  11. {  
  12.     map<string, string> trans_map;   //保存转换规则  
  13.     string key;                      //要转换的单词  
  14.     string value;                   //替换后的内容  
  15.     //读取第一个单词存入key中,行中剩余内容存入value  
  16.     while (map_file >> key && getline(map_file, value))  
  17.         if (value.size() > 1)        //检查是否有转换规则  
  18.             trans_map[key] = value.substr(1);   
  19.         else  
  20.             throw runtime_error("no rule for " + key);  
  21.     return trans_map;  
  22. }  
  23. const string &transform(const string &s, const map<string, string> &m)  
  24. {  
  25.     auto map_it = m.find(s);  
  26.   
  27.     if (map_it != m.cend())        //如果单词在转换规则m中  
  28.         return map_it->second;     //使用替换短语  
  29.     else  
  30.         return s;                  //否则返回原string  
  31. }  
  32. void word_transform(ifstream &map_file, ifstream &input)  
  33. {  
  34.     auto trans_map = buildMap(map_file);   //保存转换规则  
  35.     cout << "转换规则为: \n";  
  36.     for (auto entry : trans_map)  
  37.         cout << "key: " << entry.first<< "\tvalue: " << entry.second << endl;  
  38.     cout << "\n\n";  
  39.     string text;                     //保存输入中的每一行  
  40.     cout << "转换后为: \n";  
  41.     while (getline(input, text))  
  42.     {    
  43.         istringstream stream(text); //读取每一个单词  
  44.         string word;  
  45.         bool firstword = true;     //控制是否打印空格  
  46.         while (stream >> word)   
  47.         {  
  48.             if (firstword)  
  49.                 firstword = false;  
  50.             else  
  51.                 cout << " ";    
  52.             cout << transform(word, trans_map);   
  53.         }  
  54.         cout << endl;          
  55.     }  
  56. }  
  57.   
  58. int _tmain(int argc, _TCHAR* argv[])  
  59. {  
  60.     if (argc != 3)  
  61.         throw runtime_error("wrong number of arguments");  
  62.     ifstream map_file(argv[1]);    //第一个参数为rules.text文件  
  63.     if (!map_file)               
  64.         throw runtime_error("no transformation file");  
  65.     ifstream input(argv[2]);      //第二个参数为text.text文件  
  66.     if (!input)                   
  67.         throw runtime_error("no input file");  
  68.     word_transform(map_file, input);   
  69.     return 0;    
  70. }  


将rules.text和text.text文件放在E盘根目录下

 

技术分享

设置运行时参数,在项目属性里面,配置属性->调试->命令参数里面写上你的参数

技术分享

调试运行,结果如图示

技术分享

c++学习笔记——个单词转换的map程序详解

标签:

原文地址:http://www.cnblogs.com/xujian2014/p/4222582.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!