C++ Primer 答案 第十一章

本文深入解析C++标准模板库(STL)中的各种容器,包括map、vector、list、deque、set等,详细讲解了它们的特点和使用场景,并通过实例演示了如何在实际编程中灵活运用这些容器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

11.1
map : 关联容器,存放key和value
vector : 顺序容器,只存value

11.2
list: 经常在任何位置删除添加数据
vector: 没别的要求一般就用它
deque: 栈
map: 字典
set: 数学集合

11.3 and 11,4

#include <iostream>
#include <map>
#include <string>
#include <cctype>
using namespace std;

string change(string &s)
{
    string temp;
    for(auto i : s)
    {
        if(isalpha(i))
            temp += tolower(i);
    }
    return temp;
}
int main(int argc, char *argv[])
{
    map<string, size_t > word_count;
    string word;
    while (cin >> word)
    {
        if(word == "break")
            break;
        word = change(word);
        ++word_count[word];
    }
    for(auto &i : word_count)
        cout << i.first << " occurs " << i.second
        << ((i.second > 1)? " times" : " time" ) << endl;
    return 0;
}
/*输出
cat
cat,
dog
fish
Cat
break
cat occurs 3 times
dog occurs 1 time
fish occurs 1 time
*/

11.7
map<string, vector<string>> home;
home["Tom"].push_back("Jack");

11.8

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

int main(int argc, char *argv[])
{
    vector<string> words = {"dog", "cat", "apple"};
    string word = "dog";
    if(find(words.cbegin(), words.cend(), word) == words.cend())
        words.push_back(word);
    for(auto &i : words)
        cout << i << " ";
    cout << endl;
    return 0;
}

11.9
map<string, list<int>> m;

11.10
都不能
no viable overloaded operator[] for type 'map<vector<int>::iterator, int>' (aka 'map<__wrap_iter<int *>, int>')

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

11.12
vector<pair<string, int>> vec;
vec.emplace_back("sss", 1);

11.13
第一种最简单(clang-tidy)
vec.emplace_back("sss", 1);
vec.push_back({"sss", 1});
vec.push_back(make_pair("sss", 1));

11.14
map<string, vector<pair<string, string>>> m;

11.15
mapped_type :  vector<int>
key_type:      int
value_type:    pair<const int, vector<int>>

11.16
map<string, int> count = {{"dog", 1}, {"cat", 1}};
auto it = count.begin();
it->second = 3;

11.17
(1)合法
(2)不合法 set里没有push_back
no member named 'push_back' in 'std::__1::multiset<std::__1::basic_string<char>, std::__1::less<std::__1::basic_string<char> >, std::__1::allocator<std::__1::basic_string<char> > >'
        {container->push_back(__value_); return *this;}
(3)合法
(4)合法

11.18
map<string, int>::iterator map_it = word_count.begin();

11.19
multiset<Sales_data, bool (*)(const Sales_data &lhs, const Sales_data &rhs)>::iterator it = bookstore.begin();

11.20
肯定是之前的又短又简单

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

int main(int argc, char *argv[])
{
    map<string ,int> word_count;
    string word;
    while(cin >> word)
    {
        auto ret = word_count.insert({word, 1});
        if(!ret.second)
            ++ret.first->second;
    }
    for(auto i: word_count)
        cout << i.first << "occurs  " << i.second << "times"<< endl;
    return 0;
}

11.21
.first 是插入单词的迭代器   ->second 是 size_t
所以执行++, 就是对插入单词的size_t进行加1

11.22
pair<string, vector<int>> //参数类型
pair<map<string, vector<int>>::iterator, bool> //返回类型

11.23
multimap<string, vector<string>> home;

11.24
把 {0, 1} 加入map中

11.25
表面上使v[0]为1
但是vec是空的, 所以该操作无意义,打印会发现v[0]还是空的

11.26
map<string, int> m;
m["dog"] = 1;
很明显[]内是string , 返回值是int

11.27
count : 需要计数
find  :  只需要知道是否有

11.28
map<string, vector<int>>::iterator it = myset.find("abc");

11.29
upper_bound 和lower_bound 会返回map中不影响排序的第一个安全插入点
equal_range 返回两个成员都是c.end()的pair

11.30
pos.first  map中的key,该题具体指作者
pos.first->second map中的value,该题具体指作者的著作

11.31

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

int main(int argc, char *argv[])
{
    multimap<string, string> m = {{"Tom", "1"}, {"Tom", "2"}, {"Jack", "1"}};
    while(m.find("Tom") != m.end())
        m.erase(m.find("Tom"));
    while(m.find("Man") != m.end())
        m.erase(m.find("Man"));
    for(auto i : m)
        cout << i.first << "  "<<i.second << endl;
    return 0;
}
//输出:
//Jack  1


11.32

#include <iostream>
#include <map>
#include <string>
#include <set>
using namespace std;

int main(int argc, char *argv[])
{
    multimap<string, string> m = {{"Tom", "b"}, {"Tom", "c"}, {"Jack", "a"},{"Tom", "a"}};
    map<string, multiset<string>> morder;
    for(auto &i: m)
        morder[i.first].insert(i.second);
    for(auto &i : m)
        cout << i.first << "  "<<i.second << endl;//作品未排序
    cout << "\n" <<endl;
    for(auto &i : morder)
        for(auto &work : i.second)
            cout << i.first << "  "<< work << endl;//作品已排序
    return 0;
}
/*输出:
Jack  a
Tom  b
Tom  c
Tom  a


Jack  a
Tom  a
Tom  b
Tom  c
*/

11.34
会出现错误,因为该函数中map为const,不支持下标运算

11.35
下标运算每次都会更新value的值,insert只有map中不存在该key时再有效果

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

int main(int argc, char *argv[])
{
    map<string, string> m = {{"Tom", "a"}};
    m["Tom"] = "b";
    m["Tom"] = "c";
    m.insert({"Tom", "d"});
    m.insert({"Jack", "d"});
    cout << m["Tom"] << endl;
    cout << m["Jack"] << endl;
    return 0;
}
/*
c
d
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值