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


    实现功能:给定一个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 << "转换规则为:  ";  
    36.     for (auto entry : trans_map)  
    37.         cout << "key: " << entry.first<< " value: " << entry.second << endl;  
    38.     cout << " ";  
    39.     string text;                     //保存输入中的每一行  
    40.     cout << "转换后为:  ";  
    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盘根目录下

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

    调试运行,结果如图示

  • 相关阅读:
    nginx-rtmp-module搭建流媒体服务器
    rabbitmq安装
    opencv+python (3)
    linux命令
    mysql语句概览
    BUUCTF V&N-misc内存取证
    2018 HEBTUCTF 部分misc
    2020 安恒2月月赛 misc
    2018.6.1 铁三数据赛 复现
    2020 i春秋新春战疫公益赛 misc
  • 原文地址:https://www.cnblogs.com/xujian2014/p/4222582.html
Copyright © 2020-2023  润新知