【C++Primer】第11章 关联容器

第11章 关联容器

关联容器按关键字来保存和访问;与顺序容器不同

image-20220222112050443


11.1 使用关联容器

map<string, size_t> word_count;
string word;
while(cin >> word){
    ++word_count[word];
}
for(const auto& w : word_count){
    cout << w.first << "出现" << w.second << "次" << endl;
}

11.2 关联容器概述

关联容器的迭代器都是双向的

11.2.1 定义关联容器

map<string, size_t> word_count;
set<string> exclude = {"the", "but", "and", "or"};  //列表初始化
map<string, string> authors = {
    {"Joyce", "James"},
    {"Austen", "Jane"},
    {"Dickens", "Charles"}
};
vector<int> ivec = {1,1,2,2,3,3,4,4};
set<int> iset(ivec.cbegin(), ivec.cend());
set<int> miset(ivec.cbegin(), ivec.cend());
cout << ivec.size() << endl;   //8
cout << iset.size() << endl;   //4
cout << miset.size() << endl;  //8

11.2.3 pair类型

pair<string string> a;
pair<string, size_t> b;
pair<string, vector<int>> line;

pair<string, string> author{"James", "Jotces"};  //初始化

image-20220222114751858


11.3 关联容器操作

image-20220222115126415

set<string>::value_type v1;       //string
set<string>::key_type v2;         //string
map<string, int>::value_type v3;  //pair<const string, int>
map<string, int>::key_type v4;    //string
map<string, int>::mapped_type v5;  //int

11.3.1 关联容器迭代器

image-20220222115814136

我们通常不能对关联容器使用泛型算法

set类型中元素是const的

map中的元素是pair,其第一个成员是是const的

使用关联容器定义的专用find成员会比调用泛型find快得多

练习11.16

map<string, string>family;
map<const string, string>::iterator it;
it->first = "AAA";  //错误,const类型
it->second = "BBB";

练习11.17

v是一个string的vector,c是一个string的muitiset

copy(v.begin(), v.end(), inserter(c, c.end()));
copy(v.begin(), v.end(), back_inserter(c));   //错误, back_inserter创建一个使用push_back的迭代器
copy(c.begin(), c.end(), inserter(v, v.end()));
copy(c.begin(), c.end(), back_inserter(v));

练习11.18

pair<const string, size_t>::iterator map_it;

11.3.2 添加元素

set两种方式添加元素:

  • 一对迭代器
  • 列表初始化
vector<int> ivec = {2,4,6,8,2,4,6,8};
set<int> set2;
set2.insert(ivec.cbegin(), ivec.cend());  //4个元素
set2.insert({1,3,5,7,1,3,5,7});           //8个元素

map添加元素:

word_count.insert({word, 1});
word_count.insert(make_pair(word, 1));
word_count.insert(pair<string, size_t>(word, 1));
word_count.insert(map<string, size_t>::value_type(word, 1));

image-20220222124056797


11.3.3 删除元素

关联容器提供一个额外的erase操作,它接受一个key_type参数。此版本删除所有匹配给定关键字的元素

对允许重复关键字的容器,删除元素的数量可能大于1

multimap<string, string> authors;
authors.insert({"Barth, John", "Sot"});
suthors.insert({"Barth, John", "Lost"});

auto cnt = authors.erase("Barth, John");

image-20220222130120699


11.3.4 map的下标操作

image-20220222130317155

map<int, int> m;
m[0] = 1;   //添加对组元素

11.3.5 访问元素

set<int> iset = {0,1,2,3,4,5,6,7,8,9};
iset.find(1);   //返回一个迭代器,指向key==1的元素
iset.find(11);  //返回一个迭代器,其值等于iset.end()
iset.count(1);  //返回1
iset.count(11);  //返回0

如果不需要计数最好用find

image-20220222145618682

11.3.6 一个单词转换的map

//单词转换
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 else控制打印空格
            if(firstword){
                firstword = false;
            }
            else{
                cout << " ";   //单词间打印空格
            }
            
            //transform返回它的第一个参数或其转换之后的形式
            cout << transform(word, trans_map);  //打印输出单词
        }
        cout << endl;   //输出完一行
    }
}

//建立转换映射
map<string, string> buildMap(ifstream& map_file){
    map<string, string> trans_map;  //保存转换规则
    string key;
    string value;
    //读取的第一个单词存入key中,行中剩余内容存入value中
    while(map_file >> key && getline(map_file, value)){
        if(value.size() > 1){
            trans_map[key] = value.substr(1);
        }
        else{
            throw runtime_error("no rule for" + key);
        }
    }
    return trans_map;
}

//生成转换文本
const string& transform(const string& s, const map<string, string>& m){
    auto map_it = m.find(s);
    if(map_it != m.cend()){
        return map_it->second;   //找到则使用替换短语
    }
    else{
        return s;   //原string
    }
}

11.4 无序容器

  • unordered_map
  • unordered_set

image-20220222153056293

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zdb呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值