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

C++ //练习 11.38 用unordered_map重写单词计数程序(参见11.1节,第375页)和单词转换程序(参见11.3.6节,第391页)。

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

练习 11.38 用unordered_map重写单词计数程序(参见11.1节,第375页)和单词转换程序(参见11.3.6节,第391页)。

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

 

代码块
单词计数程序
/*************************************************************************> File Name: ex11.38a.cpp> Author: > Mail: > Created Time: Mon 08 Apr 2024 09:42:21 AM CST************************************************************************/#include<iostream>
#include<iomanip>
#include<string>
#include<map>
#include<unordered_map>
#include<vector>
using namespace std;int main(){unordered_map<string, size_t> wordCount;string word;cout<<"Enter words: ";while(cin>>word){++wordCount.insert({word, 0}).first->second;if(cin.get() == '\n'){break;}}cout<<"Word Count: "<<endl;for(const auto &w : wordCount){cout<<"Word: "<<setw(8)<<left<<w.first<<" Count: "<<w.second<<endl;}return 0;
}
运行结果显示如下

在这里插入图片描述

单词转换程序
/*************************************************************************> File Name: ex11.38b.cpp> Author: > Mail: > Created Time: Mon 08 Apr 2024 09:00:00 AM CST************************************************************************/#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<map>
#include<unordered_map>
#include<iterator>
using namespace std;unordered_map<string, string> buildMap(ifstream &map_file){unordered_map<string, string> map;string key;string value;while(map_file>>key && getline(map_file, value)){if(value.size() > 1){map[key] = value.substr(1);}else{throw runtime_error("no rule for " + key);}   }return map;
}const string &transform(const string &s, const unordered_map<string, string> &m){auto it = m.find(s);if(it != m.cend()){return it->second;}else{return s;}
}void txtTransform(ifstream &map_file, ifstream &input){auto map = buildMap(map_file);string txt;while(getline(input, txt)){istringstream stream(txt);string word;bool nonspace = true;while(stream>>word){if(nonspace){nonspace = false;}else{cout<<" ";}cout<<transform(word, map);}cout<<endl;}
}int main(){ifstream map_file("map_file.txt");ifstream input("input.txt");txtTransform(map_file, input);return 0;
}
运行结果显示如下

在这里插入图片描述

http://www.lryc.cn/news/336420.html

相关文章:

  • 【示例】MySQL-4类SQL语言-DDL-DML-DQL-DCL
  • 基于SpringBoot+Vue的果蔬种植销售一体化服务平台(源码+文档+部署+讲解)
  • 数据结构面试
  • Linux 上安装 SQLite
  • C++模板初阶(个人笔记)
  • 如何用Java后端处理JS.XHR请求
  • 分布式锁-redission
  • C/C++ 自定义头文件,及头文件结构详解
  • 快速列表quicklist
  • 《MATLAB科研绘图与学术图表绘制从入门到精通》
  • Day3-struct类型、列转行、行转列、函数
  • C++设计模式:构建器模式(九)
  • OJ 【难度1】【Python】完美字符串 扫雷 A-B数对 赛前准备 【C】精密计时
  • 【Tars-go】腾讯微服务框架学习使用01--初始化服务
  • 通过pre标签进行json格式化展示,并实现搜索高亮和通过鼠标进行逐个定位的功能
  • 5分钟了解清楚【osgb】格式的倾斜摄影数据metadata.xml有几种规范
  • CCIE-10-IPv6-TS
  • 《QT实用小工具·十七》密钥生成工具
  • CSP 比赛经验分享
  • 探究“大模型+机器人”的现状和未来
  • Commitizen:规范化你的 Git 提交信息
  • 官网下载IDE插件并导入IDE
  • 三行命令解决Ubuntu Linux联网问题
  • AI大模型在自然语言处理中的应用:性能表现和未来趋势
  • 三防平板定制服务:亿道信息与个性化生产的紧密结合
  • 【备战蓝桥杯】2024蓝桥杯赛前突击省一:基础数论篇
  • golang es查询的一些操作,has_child,inner_hit,对索引内父子文档的更新
  • 精准备份:如何自动化单个MySQL数据库的备份过程
  • Green Hills 自带的MULTI调试器查看R7芯片寄存器
  • Jupyter Notbook如何安装配置并结合内网穿透实现无公网IP远程连接使用