c++ primer中文版第五版作业第十一章

仓库地址

文章目录

      • 11.1
      • 11.2
      • 11.3
      • 11.4
      • 11.5
      • 11.6
      • 11.7
      • 11.8
      • 11.9
      • 11.10
      • 11.11
      • 11.12
      • 11.13
      • 11.14
      • 11.15
      • 11.16
      • 11.17
      • 11.18
      • 11.19
      • 11.20
      • 11.21
      • 11.22
      • 11.23
      • 11.24
      • 11.25
      • 11.26
      • 11.27
      • 11.28
      • 11.29
      • 11.30
      • 11.31
      • 11.32
      • 11.33
      • 11.34
      • 11.35
      • 11.36
      • 11.37
      • 11.38

11.1

map中的元素是按 关键字 的方式成对保存的,值的访问也是依赖关键字。而vector中的元素是按它们在容器中的位置来顺序保存和访问的。

11.2

  • vector,一般在末尾作添加删除,元素数量变化不大,需要随机访问。
  • deque,以上条件下,如果还需要在头部频繁添加删除元素,可以选择。
  • list,元素较多,元素数量在程序运行中变化频繁,只采用遍历的方式访问元素。
  • map,有关联的许多对关键字和值。
  • set,保存许多同类型元素的集合,并按值访问。

11.3

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(void)
{string tmp;map<string,size_t> smp;while(cin>>tmp)++smp[tmp];for(auto &p:smp)cout<<p.first<<" occurs "<<p.second<<((p.second>1)?" times":" time")<<endl;return 0;
}

11.4

#include <iostream>
#include <map>
#include <string>
#include <cctype>
using namespace std;
int main(void)
{string tmp;map<string,size_t> smp;while(cin>>tmp){for(size_t index=0;index<tmp.size();){tmp[index]=tolower(tmp[index]);if(ispunct(tmp[index]))tmp.erase(index,1);elseindex++;}++smp[tmp];}for(auto &p:smp)cout<<p.first<<" occurs "<<p.second<<((p.second>1)?" times":" time")<<endl;return 0;
}

11.5

map存储的是键值对,而set只存储关键字,所以需要依据关键字查找对应值时使用map,而只需要单纯检查关键字是否存在时选用set

11.6

 需要按顺序访问及储存元素选择list,而需要快速判断该元素是否在集合中时选择set

11.7

#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
void addfamily(map<string,vector<string>> &fmap,const string &surname)
{fmap[surname];
}
void addchild(map<string,vector<string>> &fmap,const string &surname,const string &child)
{fmap[surname].push_back(child);
}
int main(void)
{map<string,vector<string>> fmap;addfamily(fmap,"wang");addchild(fmap,"zhou","yi");addchild(fmap,"zhou","er");for(const auto &p1:fmap){cout<<p1.first<<"家:";for(const auto &p2:p1.second)cout<<p2<<" ";cout<<endl;}return 0;
}

11.8

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<string>::iterator save_word(vector<string> &svec,const string &str)
{vector<string>::iterator svec_it=find(svec.begin(),svec.end(),str);if(svec_it==svec.end()){svec.push_back(str);return svec.end()-1;}elsereturn svec_it;
}//返回值为指向插入或已存在单词的迭代器
int main(void)
{string tmp;vector<string> svec;while(cin>>tmp)save_word(svec,tmp);for(const auto &p:svec)cout<<p<<" ";cout<<endl;return 0;
}

 使用vector要保存不重复的单词,需要利用泛型算法排除重复项,而set则由模板自己完成这项工作。

11.9

map<string,list<unsigned>> word_rownum;

11.10

 可以定义vector<int>::iteratorintmap,因为此类型的迭代器支持<操作;不能定义list<int>::iteratorintmap,因为此类型的迭代器不支持<操作。

11.11

multiset<Sales_data,bool (*) (const Sales_data &,const Sales_data &)> bookstore(compareIsbn);

11.12

#include <iostream>
#include <sstream>
#include <utility>
#include <vector>
#include <string>
using namespace std;
bool isnum(const string &s)
{istringstream is(s);double d;char c;if(!(is>>d))return false;if(is>>c)return false;return true;
}
int main(void)
{string stmp;vector<string> stmp_vec;int itmp;vector<int> itmp_vec;vector<pair<string,int>> pvec;cout<<"enter some string or int:";while(cin>>stmp){if(isnum(stmp)){istringstream is(stmp);is>>itmp;itmp_vec.push_back(itmp);}elsestmp_vec.push_back(stmp);}for(size_t index=0;index<stmp_vec.size()&&index<itmp_vec.size();++index){pvec.push_back(pair<string,int>(stmp_vec[index],itmp_vec[index]));}for(const auto &p:pvec)cout<<p.first<<"	"<<p.second<<endl;return 0;
}

11.13

#include <iostream>
#include <sstream>
#include <utility>
#include <vector>
#include <string>
using namespace std;
bool isnum(const string &s)
{istringstream is(s);double d;char c;if(!(is>>d))return false;if(is>>c)return false;return true;
}
int main(void)
{string stmp;vector<string> stmp_vec;int itmp;vector<int> itmp_vec;vector<pair<string,int>> pvec;cout<<"enter some string or int:";while(cin>>stmp){if(isnum(stmp)){istringstream is(stmp);is>>itmp;itmp_vec.push_back(itmp);}elsestmp_vec.push_back(stmp);}for(size_t index=0;index<stmp_vec.size()&&index<itmp_vec.size();++index){//pvec.push_back(pair<string,int>(stmp_vec[index],itmp_vec[index]));//第一种方法pvec.push_back({stmp_vec[index],itmp_vec[index]});//第二种方法//pvec.push_back(make_pair(stmp_vec[index],itmp_vec[index]));//第三种方法}for(const auto &p:pvec)cout<<p.first<<"	"<<p.second<<endl;return 0;
}

11.14

#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
void addfamily(map<string,vector<pair<string,string>>> &fmap,const string &surname)
{fmap[surname];
}
void addchild(map<string,vector<pair<string,string>>> &fmap,const string &surname,const string &child,const string &birthday)
{fmap[surname].push_back({child,birthday});
}
int main(void)
{map<string,vector<pair<string,string>>> fmap;addfamily(fmap,"wang");addchild(fmap,"zhou","yi","1988.1.1");addchild(fmap,"zhou","er","1989.1.2");for(const auto &p1:fmap){cout<<p1.first<<"家:";for(const auto &p2:p1.second)cout<<p2.first<<" "<<p2.second<<"	";cout<<endl;}return 0;
}

11.15

 对一个intvector<int>的map,其mapped_typevector<int>,key_typeintvalue_typepair<const int,vector<int>>

11.16

map<int,int>::iterator map_it=test_map.begin();map_it->second=value;

11.17

copy(v.begin(),v.end(),inserter(c,c.end()));合法,调用multisetinsert成员函数,将vector中的元素,插入到c中。
copy(v.begin(),v.end(),back_inserter(c));不合法,因为multiset没有push_back成员函数。
copy(c.begin(),c.end(),inserter(v,v.end()));合法,调用vectorinsert成员函数,将c中的元素插入v的末尾。
copy(c.begin(),c.end(),back_inserter(v));合法,调用vectorpush_back成员函数,将c中的元素插入v的末尾。

11.18

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(void)
{string tmp;map<string,size_t> smp;while(cin>>tmp)++smp[tmp];for(map<string,size_t>::iterator map_it=smp.begin();map_it!=smp.end();++map_it)cout<<map_it->first<<" occurs "<<map_it->second<<((map_it->second>1)?" times":" time")<<endl;return 0;
}

11.19

multiset<Sales_data,bool (*)(const Sales_data &,const Sales_data &)>::iterator mlset_it=bookstore.begin();

11.20

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(void)
{string tmp;map<string,size_t> smp;while(cin>>tmp){pair<map<string,size_t>::iterator,bool> ret=smp.insert({tmp,1});if(!ret.second)++ret.first->second;}for(auto &p:smp)cout<<p.first<<" occurs "<<p.second<<((p.second>1)?" times":" time")<<endl;return 0;
}

11.21

 word从标准输入读取字符串,并用word作为关键字,0作为值,构成pair插入word_count中。如果插入成功,则将插入的元素的值自增1.如果插入不成功则将相同关键字的值自增1.

11.22

pair<map<string,vector<int>>::iterator,bool> insert(pair<string,vector<int>>);

11.23

#include <iostream>
#include <map>
#include <string>
using namespace std;
void addchild(multimap<string,string> &smulmp,const string &surname,const string &child)
{smulmp.insert({surname,child});
}
void pt(multimap<string,string> &smulmp)
{for(auto it=smulmp.begin();it!=smulmp.end();++it)cout<<it->first<<" "<<it->second<<endl;
}
int main(void)
{multimap<string,string> familys;addchild(familys,"zhou","yi");addchild(familys,"wang","yi");addchild(familys,"zhou","er");pt(familys);return 0;
}

11.24

 如果关键字0在m中,则将其对应的值修改为1.如果关键字0不在m中,则向m中添加一个关键字为0,对应值为0的元素,然后将此元素的值修改为1.

11.25

 如果v中含有元素,则将v[0]赋值1.如果v是空的,则会产生严重错误。

11.26

可以用key_type类型对一个map进行下标操作,下标运算符返回的类型是mapped_type;例如map<string,int>,就可以用string类型对它进行下标操作,下标运算符返回的类型是int

11.27

 只针对一个特定元素进行处理时,如在不允许重复关键字的关联容器中,选用find更合适。当要知道关联容器中有多少个元素的关键字与特定元素相等时,选用count

11.28

map<string,vector<int>>::iterator it=smap.find(key);

11.29

 如果给定关键字不在容器中,upper_boundlower_bound会返回相同的迭代器,迭代器会指向第一个不影响排序的关键字插入位置,而equal_range会返回一个pair类型的对象,其first成员和second成员都会指向第一个不影响排序的关键字插入位置。

11.30

 pos是equal_range返回的pair对象,pos的first成员是指向第一个匹配search_item关键字的map中的元素,而该元素的second成员,即pos.first->second则是关联容器中search_item关键字对应的值。

11.31

#include <iostream>
#include <map>
#include <string>
using namespace std;
void addbook(multimap<string,string> &books,const string &literature,const string &author)
{books.insert({literature,author});
}
void delbook(multimap<string,string> &books,const string &literature)
{auto it=books.find(literature);if(it==books.end())cout<<"Can't find the book "<<literature<<endl;else{books.erase(it);cout<<"The book "<<literature<<" is removed"<<endl;}
}
void printbook(const multimap<string,string> &books)
{for(auto it=books.begin();it!=books.end();++it)cout<<"author: "<<it->second<<" literature: "<<it->first<<endl;
}
int main(void)
{string liter,auth;multimap<string,string> books;cout<<"Enter literature and author(q to quit):";while(cin>>liter&&liter!="q"){cin>>auth;addbook(books,liter,auth);}cout<<"Enter literature to delete(q to quit):";while(cin>>liter&&liter!="q")delbook(books,liter);printbook(books);return 0;
}

11.32

#include <iostream>
#include <map>
#include <string>
using namespace std;
void addbook(multimap<string,string> &books,const string &author,const string &literature)
{books.insert(make_pair(author,literature));
}
void delbook(multimap<string,string> &books,const string &author,const string &literature)
{pair<multimap<string,string>::iterator,multimap<string,string>::iterator> pair_it=books.equal_range(author);if(pair_it.first==pair_it.second)cout<<"There is no author named "<<author<<endl;else{for(auto beg=pair_it.first,ed=pair_it.second;beg!=ed;++beg){if(beg->second==literature){books.erase(beg);cout<<"The literature "<<literature<<" writed by "<<author<<" was deleted"<<endl;return;}}cout<<"There is no book named "<<literature<<" writed by "<<author<<endl;}
}
void printbook(const multimap<string,string> &books)
{for(auto it=books.begin();it!=books.end();++it)cout<<"author: "<<it->first<<" literature: "<<it->second<<endl;
}
int main(void)
{multimap<string,string> books;string auth,liter;	cout<<"Enter author and literature(q to quit):";while(cin>>auth&&auth!="q"){cin>>liter;addbook(books,auth,liter);}cout<<"Enter author and literature to delete(q to quit):";while(cin>>auth&&auth!="q"){cin>>liter;delbook(books,auth,liter);}printbook(books);return 0;
}

11.33

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <stdexcept>
using namespace std;
map<string,string> buildMap(ifstream &map_file)
{map<string,string> trans_map;string key,value;while(map_file>>key&&getline(map_file,value)){if(value.size()>1)trans_map[key]=value.substr(1);elsethrow runtime_error("no rule for "+key);}return trans_map;
}
const string & transform(const string &str,map<string,string> &m)
{auto map_it=m.find(str);if(map_it==m.end())return str;elsereturn map_it->second;
}
void word_transform(ifstream &map_file,ifstream &input)
{auto trans_map=buildMap(map_file);string text;while(getline(input,text)){istringstream stream(text);string word;bool firstword=true;while(stream>>word){if(firstword)firstword=false;elsecout<<" ";cout<<transform(word,trans_map);}cout<<endl;}
}
int main(int argc,char *argv[])//第一个参数是map文件,第二个参数是源文本文件
{ifstream map_file(argv[1]);ifstream input(argv[2]);word_transform(map_file,input);return 0;
}

11.34

 如果map中含有该关键字,那么行为与原来的一样,如果map中不含该关键字,则会返回一个空的字符串。

11.35

 如果map中没有该关键字,那么前后行为一致。如果map中含有该关键字,那么下标操作会改变该关键字对应的值,而insert操作则什么都不做,并返回一个pair对象。该pair对象的first成员为指向现有关键字的迭代器,而second成员则是一个值为false的bool量,代表插入不成功。

11.36

 书中程序处理了这种情况的,如果文件中的某一行包含一个关键字和一个空格,那么这一行的转换规则不会录入map。

11.37

 在关键字类型的元素没有明显的序关系的情况下,使用无序容器,通常性能更好。而有序容器的优势在于维护了关键字的序。

11.38

//单词计数程序
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
int main(void)
{unordered_map<string,size_t> word_count;string tmp;while(cin>>tmp)++word_count[tmp];for(const auto &elemt:word_count){cout<<elemt.first<<" occurs "<<elemt.second<<(elemt.second>1?" times":" time")<<endl;}return 0;
}
//单词转换程序
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <stdexcept>
using namespace std;
unordered_map<string,string> buildMap(ifstream &map_file)
{unordered_map<string,string> trans_map;string key,value;while(map_file>>key&&getline(map_file,value)){if(value.size()>1)trans_map[key]=value.substr(1);elsethrow runtime_error("no rule for "+key);}return trans_map;
}
const string & transform(const string &str,unordered_map<string,string> &m)
{auto map_it=m.find(str);if(map_it==m.end())return str;elsereturn map_it->second;
}
void word_transform(ifstream &map_file,ifstream &input)
{auto trans_map=buildMap(map_file);string text;while(getline(input,text)){istringstream stream(text);string word;bool firstword=true;while(stream>>word){if(firstword)firstword=false;elsecout<<" ";cout<<transform(word,trans_map);}cout<<endl;}
}
int main(int argc,char *argv[])//第一个参数是map文件,第二个参数是源文本文件
{ifstream map_file(argv[1]);ifstream input(argv[2]);word_transform(map_file,input);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/731696.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Excel转pdf

1、excel-内存值--Workbook 转pdf /** * excel To pdf * * param outPath 输出路径 * param workbook excel-内存值 * throws IOException */ public static void excelToPdf(String outPath,Workbook workbook) throws IOException, DocumentException { Document documentnul…

突然发现一个很炸裂的平台!

平时小孟会开发很多的项目&#xff0c;很多项目不仅开发的功能比较齐全&#xff0c;而且效果比较炸裂。 今天给大家介绍一个我常用的平台&#xff0c;因含低代码平台&#xff0c;开发相当的快。 1&#xff0c;什么是低代码 低代码包括两种&#xff0c;一种低代码&#xff0c;…

探索JavaScript宝库:打开基础知识与实用技能之门(数据类型与变量+ 条件与循环+函数与模块+DOM+异常+ES6)

目录 [TOC](目录)一、JavaScript的基础知识1. 数据类型与变量2. 条件与循环3. 函数与模块 二、JavaScript的实用技能1. DOM操作与事件处理2. 异步编程与Promise3. ES6语法 三、JavaScript的重要性与应用场景结语 欢迎阅读本篇博客&#xff0c;我们将深入探讨JavaScript语言的基…

你知道利用神秘顾客工具提升营业厅服务水平

利用神秘顾客工具提升营业厅服务水平是一个有效的策略&#xff0c;以下是一些建议&#xff1a; 1、确定评估指标和标准&#xff1a;在利用神秘顾客工具进行调查之前&#xff0c;需要明确评估服务的指标和标准。这些指标应该根据营业厅的服务特点和重要性&#xff0c;例如服务态…

Java中的常用类(三)

一、正则表达式 正则表达式 regex&#xff0c;全称Regular Expression。正则表达式是一种规则&#xff08;模式&#xff09;匹配语法 可以使用一些正则表达式中的特殊符号来定义一种规则&#xff0c;然后用此规则匹配某个字符&#xff0c;如果字符串与规则匹配则返回true&…

【Docker1】Docker镜像和容器基本操作

Docker基本管理一、Docker概述1、为什么要用到容器&#xff1f;2、Docker是什么&#xff1f;3、Docker的设计宗旨4、容器的优点5、Docker与虚拟机的区别6、Docker的三大核心概念 二、安装Docker1、yum安装2、二进制安装 三、Docker镜像创建与操作1、Docker 镜像操作1.1 搜索镜像…

【JavaEE进阶】Spring中事务的实现

文章目录 &#x1f343;前言&#x1f334;事务简介&#x1f6a9; 什么是事务?&#x1f6a9;为什么需要事务?&#x1f6a9;事务的操作 &#x1f340;Spring 中事务的实现&#x1f6a9;Spring 编程式事务&#x1f6a9;Spring声明式事务Transactional&#x1f6a9;Transactional…

数字人民币钱包(二)

文章目录 前言一 什么是数字人民币钱包&#xff1f;二 怎么开通数字人民币钱包&#xff1f;三 数字人民币钱包有哪些&#xff1f;四 数字人民币钱包升级 前言 上篇文章梳理了什么是数字人民币&#xff0c;及其特征和相关概念&#xff0c;这篇文章来整理下数字人民币钱包。数字人…

selenium常用操作汇总

本文总结使用selenium进行web/UI自动化时&#xff0c;会用到的一些常用操作。 定位元素 driver.find_element_by_xpath()#1、绝对路径 2、元素属性 3、层级和属性结合 4、使用逻辑运算符 driver.find_element_by_id()#根据id定位&#xff0c;HTML规定id属性在HTML文档中必须是唯…

勾股定理(点赞支持!谢谢)

股定理是一个基本的几何定理&#xff0c;在中国&#xff0c;《周髀算经》记载了勾股定理的公式与证明&#xff0c;相传是在商代由商高发现&#xff0c;故又有称之为商高定理&#xff1b;三国时代的蒋铭祖对《蒋铭祖算经》内的勾股定理作出了详细注释&#xff0c;又给出了另外一…

Python hashlib statistics pyecharts模块

文章目录 Python hashlib 模块Python statistics 模块Python pyecharts 模块 Python hashlib 模块 Python 的 hashlib 模块是一个提供不同哈希算法实现的库&#xff0c;比如 MD5、SHA1、SHA256 等。这个模块允许你快速计算给定数据的哈希值&#xff0c;这在需要确保数据完整性…

App拉起微信小程序参考文章

App拉起微信小程序参考文章h5页面跳转小程序-----明文URL Scheme_weixin://dl/business/?appid*appid*&path*path*&qu-CSDN博客文章浏览阅读561次&#xff0c;点赞16次&#xff0c;收藏5次。仅需两步&#xff0c;就能实现h5跳转小程序&#xff0c;明文 URL Scheme&…

vue2 vue-cli vue-router vuex

Vue2 插值表达式 利用表达式进行插值渲染&#xff0c;将数据渲染到页面中。 语法&#xff1a;{{ 表达式 }} PS&#xff1a; 使用的数据要存在支持的是表达式&#xff0c;不是语句 if、for不能在标签属性中使用{{ }} v-show和v-if v-show底层原理&#xff1a;切换css的dis…

第三方应用软件提权方法

1、Serv-u 安全性测试&#xff08;分为有配置文件有修改权限与 servUDaemon.exe 默认管理员帐号和密码没修改进行提权&#xff09; 2、FlashFXP 安全性测试&#xff08;攻击者只需通过 webshell 下载 quick.dat、sites.dat、stats.dat这三个文件进行本地替换&#xff0c;就可以…

SRC学习-成为赏金猎人

你是否对漏洞挖掘充满好奇&#xff1f;零基础或有基础但想更进一步&#xff1f;想赚取可观的漏洞赏金让自己有更大的自由度&#xff1f; 那么&#xff0c;不妨了解下土拨鼠的安全屋 这或许也是你成为漏洞赏金猎人的第一课。 逻辑漏洞挖掘手法与创新思路&#xff0c;带你突破…

@EnableWebMvc介绍和使用详细demo

EnableWebMvc是什么 EnableWebMvc 是 Spring MVC 中的一个注解&#xff0c;它用于启用 Spring MVC 框架的基本功能&#xff0c;以便你可以使用 Spring MVC 提供的特性来处理 Web 请求。 通常情况下&#xff0c;在基于 Spring Boot 的应用中&#xff0c;并不需要显式地使用 Ena…

GC--垃圾回收

目录 垃圾回收概念 什么是垃圾? 垃圾回收机制什么时候会进行GC&#xff1f;&#xff1f; 应该关心垃圾回收那些哪些区域的回收 垃圾回收相关算法 垃圾回收算法&#xff1a;[标记阶段、回收阶段] 垃圾标记阶段 标记阶段的目的 引用计数算法&#xff08;目前没有在使用&…

实现类似 Word 协同编辑--Canvas-Editor

前言 对于word的协同编辑&#xff0c;已经构思很久了&#xff0c;但是没有找到合适的插件。今天推荐基于canvas/svg 的富文本编辑器 canvas-editor&#xff0c;能实现类似word的基础功能&#xff0c;如果后续有更好的&#xff0c;也会及时更新。 Canvas-Editor效果图: 官方文…

mysql笔记:9. 数据查询

文章目录 一、SELECT语句二、简单查询1. 查询表所有数据2. 查询部分数据3. 计算结果4. 列别名5. 去除重复项6. 表别名7. LIMIT限制数据 三、WHERE子句1. 比较查询条件2. BETWEEN AND范围查询3. IN查询4. LIKE匹配5. 空数据查询6. AND多条件查询7. OR多条件查询 四、操作查询结果…

FFmpeg--AAC音频解码流程

文章目录 AAC 组成函数分析读aac帧写aac帧aac的head参数设置 运行结果 AAC 组成 AAC音频格式&#xff1a;是⼀种由MPEG-4标准定义的有损⾳频压缩格式 ADTS:是AAC音频的传输流格式 AAC音频文件的每一帧由ADTS Header和AAC Audio Data组成 每⼀帧的ADTS的头⽂件都包含了⾳频的采…