习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html

第11章 关联容器


练习11.3

  1. #include<iostream>
  2. #include<string>
  3. #include<map>
  4. using namespace std;
  5. int main() {
  6. string s;
  7. map<string, size_t> num;
  8. cout << "输入单词表:" << endl;
  9. while (cin >> s) {
  10. ++num[s];
  11. }
  12. for (const auto &i : num) {
  13. cout << i.first << " occurs " << i.second << ((i.second > 1) ? " times" : " time") << endl;
  14. }
  15. system("pause");
  16. return 0;
  17. }

练习11.4

  1. #include<iostream>
  2. #include<string>
  3. #include<map>
  4. #include<cctype>
  5. using namespace std;
  6. void process(string &s) {
  7. for (auto i = 0;i < s.size(); ++i) {
  8. if (isupper(s[i])) s[i] = tolower(s[i]);
  9. else if (ispunct(s[i])) {
  10. s.erase(i,1);
  11. }
  12. }
  13. }
  14. int main() {
  15. string s;
  16. map<string, size_t> num;
  17. cout << "输入单词表:" << endl;
  18. while (cin >> s) {
  19. process(s);
  20. ++num[s];
  21. }
  22. for (const auto &i : num) {
  23. cout << i.first << " occurs " << i.second << ((i.second > 1) ? " times" : " time") << endl;
  24. }
  25. system("pause");
  26. return 0;
  27. }

练习11.7

  1. #include<map>
  2. #include<vector>
  3. #include<iostream>
  4. #include<string>
  5. using namespace std;
  6. int main() {
  7. map<string, vector<string>> mp;
  8. string last_name, first_name;
  9. cout << "输入姓:" << endl;
  10. cin >> last_name;
  11. cout << "输入该姓的成员名字:" << endl;
  12. while (cin >> first_name) {
  13. mp[last_name].push_back(first_name);
  14. }
  15. for (const auto i : mp) {
  16. cout << "姓:" << i.first << " 有以下成员:" << endl;
  17. for (const auto j : i.second) {
  18. cout << j << " ";
  19. }
  20. cout << endl;
  21. }
  22. system("pause");
  23. return 0;
  24. }

练习11.8

  1. #include<vector>
  2. #include<iostream>
  3. #include<algorithm>
  4. using namespace std;
  5. int main() {
  6. vector<int> v = { 1,1,2,3,4,4,5,6 };
  7. auto iter = unique(v.begin(), v.end());
  8. v.erase(iter, v.end());
  9. for (auto i : v) {
  10. cout << i << " ";
  11. }
  12. cout << endl;
  13. system("pause");
  14. return 0;
  15. }

练习11.9

  1. map<string, list<size_t>> mp;

练习11.10

vector可以,因为定义了比较大小的操作,list未定义所以不行。

练习11.11

  1. using compareType = bool (*)(const Sales_data& lhs, const Sales_data& rhs);

练习11.12

  1. #include<vector>
  2. #include<utility>
  3. #include<iostream>
  4. #include<string>
  5. using namespace std;
  6. int main() {
  7. vector<pair<string,int>> vec;
  8. size_t i = 0;
  9. string s;
  10. int num;
  11. while (cin >> s >> num) {
  12. vec.push_back({ s,num });
  13. ++i;
  14. }
  15. for (auto i : vec) {
  16. cout << i.first << " " << i.second << endl;
  17. }
  18. system("pause");
  19. return 0;
  20. }

练习11.13

  1. #include<vector>
  2. #include<utility>
  3. #include<iostream>
  4. #include<string>
  5. using namespace std;
  6. int main() {
  7. vector<pair<string,int>> vec;
  8. size_t i = 0;
  9. string s;
  10. int num;
  11. while (cin >> s >> num) {
  12. //vec.push_back({ s,num }); 第一种
  13. vec.emplace_back(s, num); //第二种:最简便
  14. //vec.push_back(make_pair(str,i));//第三种
  15. ++i;
  16. }
  17. for (auto i : vec) {
  18. cout << i.first << " " << i.second << endl;
  19. }
  20. system("pause");
  21. return 0;
  22. }

练习11.14

  1. #include<map>
  2. #include<vector>
  3. #include<iostream>
  4. #include<string>
  5. #include<utility>
  6. using namespace std;
  7. int main() {
  8. map<string, vector<pair<string,string>>> mp;
  9. string last_name, first_name, birthday;
  10. cout << "输入姓:" << endl;
  11. cin >> last_name;
  12. cout << "输入该姓的成员名字和生日:" << endl;
  13. while (cin >> first_name >> birthday) {
  14. mp[last_name].emplace_back(first_name, birthday);
  15. }
  16. for (const auto i : mp) {
  17. cout << "姓:" << i.first << " 有以下成员:" << endl;
  18. for (const auto j : i.second) {
  19. cout << j.first << " " << j.second << endl;
  20. }
  21. cout << endl;
  22. }
  23. system("pause");
  24. return 0;
  25. }

练习11.15

mapped_type 是vector

key_type 是int

value_type 是pair<int,vector>

练习11.16

  1. #include<map>
  2. #include<iostream>
  3. #include<string>
  4. using namespace std;
  5. int main() {
  6. map<string, int> mp;
  7. mp["aaa"] = 1;
  8. auto iter = mp.begin();
  9. iter->second = 2;
  10. //iter->first = "bbb"; const 不允许修改
  11. cout << iter->first << " " << iter->second << endl;
  12. system("pause");
  13. return 0;
  14. }

练习11.17

第三个不合法,back_inserter()需要用到push_back(),而set没有;其余合法。

练习11.18

  1. map<const string,size_t>::iterator

练习11.19

  1. using compareType = bool (*)(const Sales_data &lhs, const Sales_data &rhs);
  2. multiset<Sales_data, compareType> bookstore(compareIsbn);
  3. multiset<Sales_data, compareType>::iterator c_it = bookstore.begin();

练习11.20

  1. #include<iostream>
  2. #include<string>
  3. #include<map>
  4. using namespace std;
  5. int main() {
  6. map<string, size_t> word_count;
  7. string word;
  8. while (cin >> word) {
  9. auto ret = word_count.insert({ word,1 });
  10. if (!ret.second) {
  11. ++ret.first->second;
  12. }
  13. }
  14. for (auto i : word_count) {
  15. cout << i.first << " " << i.second << endl;
  16. }
  17. system("pause");
  18. return 0;
  19. }

之前的更容易阅读和编写。

练习11.21

  1. word_count.insert({ word,0 }) //得到insert的返回值,是一个pair
  2. word_count.insert({ word,0 }).first //是pair的第一个成员,是一个map迭代器,指向具有给定关键字的元素
  3. word_count.insert({ word,0 }).first -> //解引用此迭代器,提取map中的元素,元素也是一个pair
  4. word_count.insert({ word,0 }).first -> second //map中元素的值部分
  5. ++word_count.insert({ word,0 }).first->second //递增此值

练习11.23

  1. #include<map>
  2. #include<vector>
  3. #include<iostream>
  4. #include<string>
  5. using namespace std;
  6. int main() {
  7. multimap<string, vector<string>> mp;
  8. vector<string> vs;
  9. string last_name, first_name;
  10. cout << "总共有几个姓:" << endl;
  11. int n, k = 1;
  12. cin >> n;
  13. while (n--) {
  14. cout << "请输入第" << k << "个姓:" << endl;
  15. cin >> last_name;
  16. cout << "请输入姓" << last_name << "的成员,并以 END 结束当前输入" << endl;
  17. while (cin >> first_name) {
  18. if(first_name!="END") vs.push_back(first_name);
  19. else break;
  20. }
  21. mp.insert({ last_name,vs });
  22. k++;
  23. }
  24. for (const auto i : mp) {
  25. cout << "姓:" << i.first << " 有以下成员:";
  26. for (const auto j : i.second) {
  27. cout << j << " ";
  28. }
  29. cout << endl;
  30. }
  31. system("pause");
  32. return 0;
  33. }

练习11.28

  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. #include<map>
  5. using namespace std;
  6. int main() {
  7. map<string, vector<int>> mp = { { "aa",{1,2,3,4} } };
  8. auto iter = mp.find("aa");
  9. cout << iter->first << endl;
  10. system("pause");
  11. return 0;
  12. }

练习11.29

upper_bound和lower_bound返回相等的迭代器——指向一个不影响排序的关键字插入位置。

练习11.30

第一个迭代器引用后得到书名。

练习11.31

  1. #include<iostream>
  2. #include<map>
  3. #include<string>
  4. using namespace std;
  5. int main() {
  6. string search_item = "bbb";
  7. multimap < string, string > mp = { {"John", "aaa"}, { "John","bbb" }, { "John","ccc" },{"Tom","ddd"} };
  8. auto iter = mp.find("John");
  9. auto num = mp.count("John");
  10. while (num) {
  11. if (iter->second == search_item) {
  12. mp.erase(iter);
  13. break;
  14. }
  15. ++iter;
  16. --num;
  17. }
  18. for (auto i : mp) {
  19. cout << i.first << " " << i.second << endl;
  20. }
  21. system("pause");
  22. return 0;
  23. }

练习11.32

  1. #include<iostream>
  2. #include<map>
  3. #include<string>
  4. #include<set>
  5. using namespace std;
  6. int main() {
  7. string search_item = "bbb";
  8. multimap < string, string > mp = { {"John", "baa"}, { "John","abb" }, { "John","ced" },{"Tom","ccc"},{"Bob","zzz"},{"Bob","aae"} };
  9. map<string, set<string>> ans;
  10. for (auto i : mp) {
  11. ans[i.first].insert(i.second);
  12. }
  13. for (auto i : ans) {
  14. cout << i.first << " : ";
  15. for (auto j : i.second) {
  16. cout << j << " ";
  17. }
  18. cout << endl;
  19. }
  20. system("pause");
  21. return 0;
  22. }

练习11.33

  1. #include<iostream>
  2. #include<string>
  3. #include<map>
  4. #include<fstream>
  5. #include<sstream>
  6. using namespace std;
  7. //函数buildMap读入给定文件,建立起转换映射
  8. map<string, string> buildMap(ifstream &map_file) {
  9. map<string, string> trans_map;
  10. string key;
  11. string value;
  12. //读取第一个单词存入key中,行中剩余内容存入value
  13. while (map_file >> key && getline(map_file, value)) {
  14. if (value.size() > 1) {
  15. trans_map[key] = value.substr(1); //跳过前导空格
  16. }
  17. else {
  18. throw runtime_error("no rule for " + key);
  19. }
  20. }
  21. return trans_map;
  22. }
  23. //生成文本转换
  24. const string & transform(const string &s, const map<string, string> &m) {
  25. auto map_it = m.find(s);
  26. if (map_it != m.cend()) {
  27. return map_it->second;
  28. }
  29. else {
  30. return s;
  31. }
  32. }
  33. void word_transform(ifstream &map_file, ifstream &input) {
  34. auto trans_map = buildMap(map_file);//保存转换规则
  35. string text;
  36. while (getline(input, text)) {
  37. istringstream stream(text); //读取每个单词
  38. string word;
  39. bool firstword = true; //控制是否打印空格
  40. while (stream >> word) {
  41. if (firstword) {
  42. firstword = false;
  43. }
  44. else cout << " ";
  45. //transform返回它的第一个参数或其转换之后的形式
  46. cout << transform(word, trans_map);
  47. }
  48. cout << endl;
  49. }
  50. }
  51. int main() {
  52. ifstream map_file("book.txt"), input("test.txt");
  53. word_transform(map_file, input);
  54. system("pause");
  55. return 0;
  56. }

练习11.34

下标操作,当元素不存在时会隐式地创建一个新元素插入到map中,修改了map

练习11.35

如果有重复的key,则下标表示时新的会替换旧的,而insert不会,那么在transform函数中find会有多个返回值导致出错。

练习11.36

  1. #include<iostream>
  2. #include<string>
  3. #include<map>
  4. #include<fstream>
  5. #include<sstream>
  6. using namespace std;
  7. //函数buildMap读入给定文件,建立起转换映射
  8. map<string, string> buildMap(ifstream &map_file) {
  9. map<string, string> trans_map;
  10. string key;
  11. string value;
  12. //读取第一个单词存入key中,行中剩余内容存入value
  13. while (map_file >> key && getline(map_file, value)) {
  14. if (value.size() > 1) {
  15. trans_map[key] = value.substr(1); //跳过前导空格
  16. }
  17. else {
  18. throw runtime_error("no rule for " + key);
  19. }
  20. }
  21. return trans_map;
  22. }
  23. //生成文本转换
  24. const string & transform(const string &s, const map<string, string> &m) {
  25. auto map_it = m.find(s);
  26. if (map_it != m.cend()) {
  27. return map_it->second;
  28. }
  29. else {
  30. return s;
  31. }
  32. }
  33. void word_transform(ifstream &map_file, ifstream &input) {
  34. auto trans_map = buildMap(map_file);//保存转换规则
  35. string text;
  36. while (getline(input, text)) {
  37. istringstream stream(text); //读取每个单词
  38. string word;
  39. bool firstword = true; //控制首个空格
  40. while (stream >> word) {
  41. if (firstword) {
  42. firstword = false;
  43. }
  44. else cout << " ";
  45. //transform返回它的第一个参数或其转换之后的形式
  46. cout << transform(word, trans_map);
  47. }
  48. cout << endl;
  49. }
  50. }
  51. int main() {
  52. ifstream map_file("book.txt"), input("test.txt");
  53. word_transform(map_file, input);
  54. system("pause");
  55. return 0;
  56. }

练习11.37

无序容器不用排序,效率高。

有序容器可以自定义排序。

练习11.38

单词计数程序

  1. #include <unordered_map>
  2. #include <string>
  3. #include <iostream>
  4. using namespace std;
  5. int main()
  6. {
  7. unordered_map<string, size_t> word_count;
  8. string word;
  9. while(cin >> word)
  10. ++word_count[word];
  11. for(const auto &w : word_count)
  12. cout << w.first << "," << w.second << endl;
  13. return 0;
  14. }

单词转换程序

  1. #include<iostream>
  2. #include<string>
  3. #include<unordered_map>
  4. #include<fstream>
  5. #include<sstream>
  6. using namespace std;
  7. //函数buildMap读入给定文件,建立起转换映射
  8. unordered_map<string, string> buildMap(ifstream &map_file) {
  9. unordered_map<string, string> trans_map;
  10. string key;
  11. string value;
  12. //读取第一个单词存入key中,行中剩余内容存入value
  13. while (map_file >> key && getline(map_file, value)) {
  14. if (value.size() > 1) {
  15. trans_map[key] = value.substr(1); //跳过前导空格
  16. }
  17. else {
  18. throw runtime_error("no rule for " + key);
  19. }
  20. }
  21. return trans_map;
  22. }
  23. //生成文本转换
  24. const string & transform(const string &s, const unordered_map<string, string> &m) {
  25. auto map_it = m.find(s);
  26. if (map_it != m.cend()) {
  27. return map_it->second;
  28. }
  29. else {
  30. return s;
  31. }
  32. }
  33. void word_transform(ifstream &map_file, ifstream &input) {
  34. auto trans_map = buildMap(map_file);//保存转换规则
  35. string text;
  36. while (getline(input, text)) {
  37. istringstream stream(text); //读取每个单词
  38. string word;
  39. bool firstword = true; //控制首个空格
  40. while (stream >> word) {
  41. if (firstword) {
  42. firstword = false;
  43. }
  44. else cout << " ";
  45. //transform返回它的第一个参数或其转换之后的形式
  46. cout << transform(word, trans_map);
  47. }
  48. cout << endl;
  49. }
  50. }
  51. int main() {
  52. ifstream map_file("book.txt"), input("test.txt");
  53. word_transform(map_file, input);
  54. system("pause");
  55. return 0;
  56. }

C++Primer第五版——习题答案详解(十)的更多相关文章

  1. C++Primer第五版——习题答案详解(一)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第1章 开始&&第2章 变量和基本类型 练习1.3 #include&l ...

  2. C++Primer第五版——习题答案详解(二)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第3章 字符串.向量和数组 练习3.2 一次读入一整行 #include<iost ...

  3. C++Primer第五版——习题答案详解(三)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第4章 表达式 练习4.10 while(cin>>i&&i ...

  4. C++Primer第五版——习题答案详解(四)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第5章 语句 练习5.9 #include<iostream> #inclu ...

  5. C++Primer第五版——习题答案详解(五)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第6章 函数 练习6.4 #include<iostream> using ...

  6. C++Primer第五版——习题答案详解(六)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第7章 类 练习7.1 class Sales_data { public: std:: ...

  7. C++Primer第五版——习题答案详解(七)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第8章 IO库 练习8.1 istream &iofunc(istream &a ...

  8. C++Primer第五版——习题答案详解(八)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第9章 顺序容器 练习9.1 a.list,需要按字典序插入,可能插入位置在中间 b.d ...

  9. C++Primer第五版——习题答案详解(九)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第10章 泛型算法 练习10.1 #include<iostream> #i ...

随机推荐

  1. GoEasy的使用

    GoEasy介绍 http请求短连接,一次请求响应后关闭,而GoEasy建立了客户端与服务器之间的长连接. goeasy支持服务器到客户端的消息发布,客户端到客户端的消息发布 GoEasy用来做什么 ...

  2. Fedora 23 U盘启动出现“Failed to load ldlinux.c32”解决

    利用UltraISO制作了Fedora 23的U盘启动,开机F12键USB启动时出现 Failed to load ldlinux.c32 Boot failed: please change dis ...

  3. EOS使用

    公司要玩区块链,听说EOS交易快,就弄来玩玩.弄了一天终于编译成功了. Scanning dependencies of target nodeos [%] Building CXX object p ...

  4. Eclipse+Maven+Scala Project+Spark | 编译并打包wordcount程序

    学习用Eclipse+Maven来构建并打包一个简单的单词统计的例程. 本项目源码已托管于Github –>[Spark-wordcount] 第一步 在EclipseIDE中安装Scala插件 ...

  5. C++解析十-数据封装

    数据封装 所有的 C++ 程序都有以下两个基本要素: 程序语句(代码):这是程序中执行动作的部分,它们被称为函数. 程序数据:数据是程序的信息,会受到程序函数的影响.封装是面向对象编程中把数据和操作数 ...

  6. SpringBoot使用CORS解决跨域请求问题

    什么是跨域? 同源策略是浏览器的一个安全功能,不同源的客户端脚本在没有明确授权的情况下,不能读写对方资源. 同源策略是浏览器安全的基石. 如果一个请求地址里面的协议.域名和端口号都相同,就属于同源. ...

  7. CSS3 Transform的perspective属性

    以下两行语句有什么区别? Css <div id="animateTest" style="-webkit-transform: perspective(400px ...

  8. LinuxTimeLine

  9. 2D过渡模块的其他属性

    官网上关于过渡属性的值: 属性 描述 CSS transition 简写属性,用于在一个属性中设置四个过渡属性. 3 transition-property 规定应用过渡的 CSS 属性的名称. 3 ...

  10. Windows平台下不同版本SVN对比

    (1)SVN服务端subversion与SVN客户端tortoiseSVN (2)subversion服务器程序在windows下共有5个下载版本,分别是:Collabnet , SlikSVN , ...