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

C++ Primer第五版_第十一章习题答案(31~38)

文章目录

      • 练习11.31
      • 练习11.32
      • 练习11.33
      • 练习11.34
      • 练习11.25
      • 练习11.36
      • 练习11.37
      • 练习11.38

练习11.31

编写程序,定义一个作者及其作品的multimap。使用find在multimap中查找一个元素并用erase删除它。确保你的程序在元素不在map 中时也能正常运行。

#include <map>
#include <string>
#include <iostream>

using std::string;

int main()
{
	std::multimap<string, string> authors{
		{ "alan", "DMA" },
		{ "pezy", "LeetCode" },
		{ "alan", "CLRS" },
		{ "wang", "FTP" },
		{ "pezy", "CP5" },
		{ "wang", "CPP-Concurrency" } };

	string author = "pezy";
	string work = "CP5";

	auto found = authors.find(author);
	auto count = authors.count(author);
	while (count)
	{
		if (found->second == work)
		{
			authors.erase(found);
			break;
		}
		++found;
		--count;
	}

	for (const auto &author : authors)
		std::cout << author.first << " " << author.second << std::endl;

	return 0;
}

练习11.32

使用上一题定义的multimap 编写一个程序,按字典序打印作者列表和他们的作品。

#include <map>
#include <set>
#include <string>
#include <iostream>

using std::string;

int main()
{
	std::multimap<string, string> authors{
		{ "alan", "DMA" },
		{ "pezy", "LeetCode" },
		{ "alan", "CLRS" },
		{ "wang", "FTP" },
		{ "pezy", "CP5" },
		{ "wang", "CPP-Concurrency" } };
	std::map<string, std::multiset<string>> order_authors;

	for (const auto &author : authors)
		order_authors[author.first].insert(author.second);

	for (const auto &author : order_authors)
	{
		std::cout << author.first << ": ";
		for (const auto &work : author.second)
			std::cout << work << " ";
		std::cout << std::endl;
	}

	return 0;
}

练习11.33

实现你自己版本的单词转换程序。

#include <iostream>
#include <map>
#include <fstream>
#include <sstream>

using namespace std;

void word_transform(ifstream&, ifstream&);
map<string, string> buildMap(ifstream&);
string transform(const string&, map<string, string>&);

int main()
{
	ifstream ifs_rules("./data/transform_rules.txt");
	ifstream ifs_txt("./data/for_transform.txt");
	
	word_transform(ifs_rules, ifs_txt);
	return 0;
}

void word_transform(ifstream& rule_file, ifstream& input)
{
	auto rule_map = buildMap(rule_file);
	string text;
	while (getline(input, text))
	{
		istringstream stream(text);
		string word;
		bool firstword = true;
		while (stream >> word)
		{
			if (firstword)
				firstword = false;
			else
				cout << " ";
			cout << transform(word, rule_map);
		}
		cout << endl;
	}
}

map<string, string> buildMap(ifstream& rule_file)
{
	map<string, string> m;
	string key;
	string value;
	while (rule_file >> key && getline(rule_file, value))
	{
		if (value.size() > 1)
			m[key] = value.substr(1);
		else
			throw runtime_error("no rule for " + key);
	}
	return m;
}

string transform(const string& s, map<string, string>& m)
{
	auto it = m.find(s);
	if (it != m.cend())
		return it->second;
	else
		return s;
}

练习11.34

如果你将transform 函数中的find替换为下标运算符,会发生什么情况?

如果使用下标运算符,当关键字未在容器中时,会往容器中添加一个新元素。

练习11.25

在buildMap中,如果进行如下改写,会有什么效果?

trans_map[key] = value.substr(1);
//改为
trans_map.insert({key, value.substr(1)});

当一个转换规则的关键字多次出现的时候,使用下标运算符会保留最后一次添加的规则,而用insert则保留第一次添加的规则。

练习11.36

我们的程序并没检查输入文件的合法性。特别是,它假定转换规则文件中的规则都是有意义的。如果文件中的某一行包含一个关键字、一个空格,然后就结束了,会发生什么?预测程序的行为并进行验证,再与你的程序进行比较。

如果关键字没有对应的规则,那么程序会抛出一个 runtime_error

练习11.37

一个无序容器与其有序版本相比有何优势?有序版本有何优势?

无序容器拥有更好的性能,有序容器使得元素始终有序。

练习11.38

用 unordered_map 重写单词计数程序和单词转换程序。

#include <iostream>
#include <unordered_map>
#include <fstream>
#include <sstream>

using namespace std;

void word_transform(ifstream&, ifstream&);
unordered_map<string, string> buildMap(ifstream&);
string transform(const string&, unordered_map<string, string>&);

int main()
{
	ifstream ifs_rules("H:/code/C++/Cpp_Primer_Answers/data/transform_rules.txt");
	ifstream ifs_txt("H:/code/C++/Cpp_Primer_Answers/data/for_transform.txt");

	word_transform(ifs_rules, ifs_txt);
	return 0;
}

void word_transform(ifstream& rule_file, ifstream& input)
{
	auto rule_map = buildMap(rule_file);
	string text;
	while (getline(input, text))
	{
		istringstream stream(text);
		string word;
		bool firstword = true;
		while (stream >> word)
		{
			if (firstword)
				firstword = false;
			else
				cout << " ";
			cout << transform(word, rule_map);
		}
		cout << endl;
	}
}

unordered_map<string, string> buildMap(ifstream& rule_file)
{
	unordered_map<string, string> m;
	string key;
	string value;
	while (rule_file >> key && getline(rule_file, value))
	{
		if (value.size() > 1)
			m[key] = value.substr(1);
		else
			throw runtime_error("no rule for " + key);
	}
	return m;
}

string transform(const string& s, unordered_map<string, string>& m)
{
	auto it = m.find(s);
	if (it != m.cend())
		return it->second;
	else
		return s;
}

http://www.kler.cn/news/9933.html

相关文章:

  • 程序员必用的6个代码对比神器附下载地址
  • Linux嵌入式学习之Ubuntu入门(二)磁盘文件介绍及分区、格式化等
  • NumPy 初学者指南中文第三版:1~5
  • 【三十天精通Vue 3】 第三天 Vue 3的组件详解
  • 一位腾讯在职7年测试工程师的心声...
  • 为什么会有JMM?从0到1一次性说清楚
  • Adaptive AUTOSAR——State Management(VRTE 3.0 R21-11)
  • 笔记 | python蓝桥算法复习(预习)基础知识
  • 快排非递归 归并排序
  • spring(七):事务操作
  • docker 安装nocas
  • 亚马逊云科技Amazon Linux 2023正式发布,将为您提供长期支持
  • 使用Xftp连接Windows7虚拟机
  • webgl-图形非矩阵旋转
  • GooglePlay马甲包过审详细流程
  • Java基础(六)面向对象编程(进阶)
  • 2023-04-14 算法面试中常见的查找表问题
  • Available-Python-Tuf
  • ChatGPT大规模封号+停止注册?最火概念会凉吗?
  • 【Camera HW介绍】
  • AI绘画——Stable Diffusion模型,变分自编码器(VAE)模型 , lora模型——调配设置与分享
  • shell的简单信息-执行、变量命名、变量的数据类型
  • git 命令:工作日常使用
  • 基于CBC、ECB、CTR、OCF、CFB模式的AES加密算法
  • 洛谷 P1341 无序字母对
  • Monitor方案MT9800学习笔记(三) —— 点屏(V-by-One信号接口)
  • MybatisPlus <= 3.5.3.1 TenantPlugin 组件 存在 sql 注入漏洞(CVE-2023-25330)
  • 测试:腾讯云轻量4核8G12M服务器CPU流量带宽系统盘
  • pytorch进阶学习(三):在数据集数量不够时如何进行数据增强
  • 花30分钟,我用ChatGPT写了一篇2000字文章(内附实操过程)