C++primer练习11.24-11.38

练习11,24

下面的程序完成什么功能呢

map<int,int> m;

m[0]=1;

::添加一个新的关键字0,并赋值1

练习11.25

对比下面的程序与上一题程序

vector<int> v;

v[0]=1;

::给第一个元素赋值1

练习11.26

可以用什么类型对一个map进行下标操作?下标运算符返回的类型是什么》请给出一个具体的例子

map<string,int> word_count;
word_count["beauty"]=1;

::关键字类型要求具有严格弱序,也就是它会对关键字进行排序,这个关键字需要有自己的排序方式,返回的是元素的second,也就是mapped_type

练习11.27

对于什么问题你会使用count来解决?什么时候你又会选择find呢?

::count用来计数,find用来判断是否存在(尽管count也可以)

练习11.28

对一个string到int的vector的map,定义并初始化一个变量来保存其上调用find所返回的结果

map<string,vector<int>> cpa;
cpa["beauty"]=(cpa.find("beauty"))->second;

练习11.29

如果给定的关键字不在容器中,upper_bound、lower_bound和equal_range分别会返回什么

::前面两个会返回尾后迭代器,最后一个返回的迭代器的pair的两个成员均为尾后迭代器

练习11.30

对于本节最后一个程序中的输出表达式,解释运算对象pos.first->second的含义

::第一个关键字为search_item的元素的值

练习11.31

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

int main()
{
multimap<string,string> portfolio{{"li","dalei"},{"bao","bolo"},{"li","port"}};
portfolio.erase(portfolio.find("li"));

 } 

练习11.32

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

int main()
{
multimap<string,string> portfolio{{"li","dalei"},{"bao","bolo"},{"li","port"}};
portfolio.erase(portfolio.find("li"));
for(auto d:portfolio)
cout<<d.first<<d.second<<endl;
 } 

 

 练习11.33

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

map<string,string> build_map(ifstream &rulefile)
{
    map<string,string> trans_map;
    string key;
    string value;
    while(rulefile>>key&&getline(rulefile,value))
    {
        
        trans_map[key]=value;
    }
    return trans_map;
}
string transform(string &s,map<string,string>&m)
{
    auto inof=m.find(s);
    if(inof==m.end())
    return s;
    return inof->second;
}
void word_transform(ifstream &rulefile,ifstream &input)
{
    auto trans_map=build_map(rulefile);
    string text;
    while(getline(input,text))
    {
        istringstream str(text);
        string word;
        bool space=true;
        while(str>>word)
        {
            if(space)
            space=false;
            cout<<transform(word,trans_map);
        }cout<<endl;
    }
}

练习11.34

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

::如果不存在,会创建一个新关键字

练习11.35

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

trans_map.insert({key,value.substr(1)})

::如果有相同关键字只保存第一个的值

练习11.36

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

::runtime_error

练习11.37

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

::无序容器在允许重复关键字时很有用,也没有维持有序关系的开销,有序版本在我想维护序和不重复关键字时派上用场

练习11.38

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

unordered_map<string,string> build_map(ifstream &rulefile)
{
    unordered_map<string,string> trans_map;
    string key;
    string value;
    while(rulefile>>key&&getline(rulefile,value))
    {
        
        trans_map[key]=value;
    }
    return trans_map;
}
string transform(string &s,unordered_map<string,string>&m)
{
    auto inof=m.find(s);
    if(inof==m.end())
    return s;
    return inof->second;
}
void word_transform(ifstream &rulefile,ifstream &input)
{
    auto trans_map=build_map(rulefile);
    string text;
    while(getline(input,text))
    {
        istringstream str(text);
        string word;
        bool space=true;
        while(str>>word)
        {
            if(space)
            space=false;
            cout<<transform(word,trans_map);
        }cout<<endl;
    }
}

 

posted @ 2022-07-30 09:20  yddl  阅读(37)  评论(0编辑  收藏  举报