当前位置: 首页 > news >正文

C++ //练习 17.33 修改11.3.6节(第392页)中的单词转换程序,允许对一个给定单词有多种转换方式,每次随机选择一种进行实际转换。

C++ Primer(第5版) 练习 17.33

练习 17.33 修改11.3.6节(第392页)中的单词转换程序,允许对一个给定单词有多种转换方式,每次随机选择一种进行实际转换。

环境:Linux Ubuntu(云服务器)
工具:vim

 

代码块
map<string, vector<string>> buildMap(ifstream &map_file){map<string, vector<string>> trans_map;string key, value;while(map_file>>key && getline(map_file, value)){if(value.size() > 1){trans_map[key];trans_map[key].push_back(value.substr(1));}else{throw runtime_error("no rule for " + key); }}return trans_map;
}const string &transform(const string &s, const map<string, vector<std::string>> &m){map<string, vector<std::string>>::const_iterator map_it = m.find(s);static default_random_engine e;if(map_it != m.cend()){if(map_it->second.size() == 1){return map_it->second[0];}else{uniform_int_distribution<unsigned> u(0, map_it->second.size()-1);return map_it->second[u(e)];}}else{return s;}
}void word_transform(ifstream &map_file, ifstream &input){map<string, vector<string>> trans_map = buildMap(map_file);string text;while(std::getline(input, text)){istringstream stream(text);string word;bool firstword = true;while(stream>>word){if(firstword){firstword = false;}else{cout << " ";}cout<<transform(word, trans_map);}cout<<endl;}
}

http://www.mrgr.cn/news/4169.html

相关文章:

  • Go小技巧易错点100例(十七)
  • 养猫劝退?猫咪掉毛严重怎么办?用宠物空气净化器高效清理浮毛
  • C++竞赛初阶L1-14-第六单元-数组(31~33课)539: T456469 与指定数字相同的数的个数
  • 代码随想录算法训练营day51:图论02:99. 岛屿数量;100. 岛屿的最大面积
  • Ruby GUI宝典:探索顶级图形界面库
  • 故障频发,给我一个完美的解释...
  • 基于Kotlin Multiplatform实现静态文件服务器(二)
  • 【taro react】 ---- 通过时间格式化处理提取倒计时的分秒、时段的小时分、选择日期的月和天
  • Springcloud从零开始---Zuul(二)
  • VAuditDemo常规漏洞
  • Docker Swarm 管理
  • Kaggle竞赛——心脏病患者预测与可视化
  • Android 12系统源码_多屏幕(三)模拟辅助设备功能实现原理
  • PCDN技术如何提高网络性能?
  • Huawei Matebook e 2022 安装 archlinux 双系统
  • 鸿蒙卡片服务
  • 第R2周:LSTM-火灾温度预测
  • 【C/C++】宏定义标志位使用到 (1U<<0)是为什么?
  • ROS常用消息之Image
  • 基于Django的停车场车辆出入管理系统,可识别车牌图片