您的位置:首页 > 编程语言 > C语言/C++

C++Primer第五版 第十一章习题答案(1~10)

2017-03-01 09:31 344 查看
1:知识点1:关联容器与顺序容器有着根本的不同

知识点2:关联容器的元素是按关键词来保存和访问的,顺序容器是按他们在容器中的位置来顺序访问

知识点3:关键词在关联容器中起到重要的作用,map中的元素是(关键字—值)对,关键词是索引左右,值是与索引相关联的数据。set每个元素只包含一个关键字,支持高效的关键词查找

知识点4:标准库提供8个关联容器,主要是map/set,是否重复关键字,顺序保存容器与否

答案见上

2:知识点1:map的定义及使用

1:包含map头文件,2:指定关键词和值的类型,3:关键词可作为下标,索引对应值

知识点2:size_t是一种机器相关的无符号类型,它被设计的足够大以便能保存内存中的任意对象的大小,在使用数组下标时,经常会用到此类型

知识点3:对map对象进行下标操作,如若关键字不在map中,下标运算符会在map中创建一个新元素(关键词),对应值为默认初始化值。

知识点4:从map对象中提取一个元素时,会得到一个pair对象,其保存两个public数据对象,first为其关键字,second为其对应值。

这几个容器的主要用法:

map:存储字典型数据

set:坏值检验,只有关键字的好处

list:任意位置任意删除添加数据

deque:信息处理,只在头部

vector:相关联数据,顺序处理

3:知识点1:set的定义与使用,与map值差一个值而已,它只有关键字

知识点2:set对find()的使用,返回一个迭代器,如果给定的关键词在set中,迭代器指向该关键词,如不在,则返回尾后迭代器

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<map>
using namespace std;

int main(int argc, char**argv)
{
//map的定义
map<string,size_t> word_count;
fstream in("1.txt");//定义一个输入流
string word;

while (in>>word)
{
++word_count[word];
}

//map同样支持迭代器操作
map<string ,size_t>::iterator mapi;
for (mapi = word_count.begin(); mapi != word_count.end(); ++mapi)//C++ 11支持:const auto &s : word_count
{
//两个成员分别代表关键字和对应值
cout<<mapi->first<<" ";
cout<<mapi->second<<" "<<endl;
}

return 0;
}


4:知识点1:头文件:#inlude <cctype.h>,ispunct() 函数用来检测一个字符是否为标点符号或特殊字符,其原型为:int ispunct(int c);

知识点2:目前在头文件iostream中也可以使用,C++ 5.11已证明。把字符转换成小写字母,非字母字符不做出处理用 法: int tolower(int c);

知识点3:Transforms the range [first,last) into a range with all the elements for which pred returns true removed, andreturns
an iterator to the new end of that range.

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<map>
#include<cctype>//ctype无法打开,包含tolower()函数和ispunct函数
#include<algorithm>
using namespace std;

int main(int argc, char**argv)
{
//map的定义
map<string,size_t> word_count;
fstream in("1.txt");//定义一个输入流
string word;

while (in>>word)
{
string::iterator it1;
for (it1 = word.begin(); it1 != word.end(); ++it1)
{
*it1 = tolower(*it1);
}//消灭大小写
word.erase(remove_if(word.begin(), word.end(), ispunct),word.end());//消灭标点符号
++word_count[word];
}

//map同样支持迭代器操作
map<string ,size_t>::iterator mapi;
for (mapi = word_count.begin(); mapi != word_count.end(); ++mapi)//C++ 11支持:const auto &s : word_count
{
//两个成员分别代表关键字和对应值
cout<<mapi->first<<" ";
cout<<mapi->second<<" "<<endl;
}

return 0;
}


5:知识点1:关联容器不支持顺序容器的位置相关操作,如:push_back、push_front,因为其是按关键字存储的

知识点2:关联容器的迭代器都是双向的,还有一些关于哈希性能的操作

知识点3:multimap和multiset允许多个元素具有相同的关键字,所以给multiset和multimap中传入相同的元素,是可行的,而set、map会忽略相同关键字的元素

答案:区别见2.3题知识点,当只需要使用关键字时使用set,需要使用元素对,则应该使用map

6:区别就是:set中的元素不可以重复、且有序。

7:注意代码中的注释

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<map>
#include<cctype>//ctype无法打开,包含tolower()函数和ispunct函数
#include<algorithm>
using namespace std;

int main(int argc, char**argv)
{
//map的定义
map<string,vector<string>> family;

string first_name,child_name;

//在while中使用lambda语句,可以传入多条语句参数,将我们想要传入的的输出参数也放在其中,注意后面的一对括号
while ( [&]()->bool {cout<<"请输入家庭的姓:"; return cin>>first_name && (first_name != "end");}() )
{
cout<<"请输入孩子的名字:";
while (cin>>child_name && (child_name != "end"))
{
family[first_name].push_back(child_name);
}
}

//map同样支持迭代器操作
map<string ,vector<string>>::iterator mapi;
for (mapi = family.begin(); mapi != family.end(); ++mapi)//C++ 11支持:const auto &s : word_count
{
//两个成员分别代表关键字和对应值
cout<<mapi->first<<" :";
vector<string>::iterator it1 = mapi->second.begin();
for (it1; it1 != mapi->second.end(); ++it1)
{
cout<<*it1<<" ";
}

}

return 0;
}


8:set会自动忽略重复的关键字,在vector中插入元素非常耗时,但可以使用push_back()

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<map>
#include<set>
#include<cctype>//ctype无法打开,包含tolower()函数和ispunct函数
#include<algorithm>
using namespace std;

int main(int argc, char**argv)
{
string a[5] = {"aa","11","00","88","shit"};
//VS2010不支持列表初始化,使用迭代器初始化
vector<string> word(a,a+5);
string word1;
while(cin>>word1)
{
if (find(word.begin(),word.end(),word1) != word.end())
{
cout<<"不可输入重复元素";
}
else
{
word.push_back(word1);
}
}

return 0;
}


9:知识点:有序容器在定义其关键字时,其关键字的类型必须包含元素比较的方法,如果是一个类类型,且没有包含比较方法,则不合法,可以自行定义比较类型。

map<string,list<size_t>> sad;//注意头文件的包含


10:vector可以,因为其迭代器定义了大小比较的操作,而list的迭代器则没有,所以不可以
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息