欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > c/c++ >内容正文

c/c++

C++ Primer 5th笔记(chap 11)关联容器操作

发布时间:2025/3/21 c/c++ 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C++ Primer 5th笔记(chap 11)关联容器操作 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1. 添加元素insert

参数返回值
m.insert(e);e为pair 返回一个pair, first指向e的一个迭代器,second 是bool值表示是否成功插入
m.emplace(args)args是参数列表
m.insert(beg,end);b e是迭代器 返回void
m.insert(il);值的花括号列表 返回void
m.insert(iter,e);iter为辅助,e为pair 返回迭代器,指向m中具有特定键的元素
m.insert(iter,args);iter为辅助,args是参数列表 返回迭代器,指向m中具有特定键的元素

eg.

string word("dda"); //map<string, size_t> m = { { "2", 0 } };map<string, size_t> word_count; word_count.insert({ word, 1 });//在参数列表中使用花括号初始化创建了一个pair。 word_count.insert(make_pair(word, 1));//也可在参数列表中调用make_pair构造pair。 word_count.insert(pair<string, size_t>(word, 1));//也可显示构造pair。 word_count.insert(map<string, size_t>::value_type(word, 1)); //先构造一个pair类型,再构造该类型的一个新对象(对象类型为pair)。map<string, size_t>::iterator a = word_count.begin(); map<string, size_t>::iterator b = ++a;cout << word_count.size() << endl; word_count.insert(a, b); word_count.insert(b, {"da", 0}); cout << word_count.size() << endl;

eg2.

向multiset或multimap添加元素 multimap<string, string> authors; authors.insert({"author1", "book1"}); authors.insert({"author1", "book2"});

2. 遍历

std::map<int, int> word_count = { {0, 100},{ 1,200}, { 2,300 },{4,400} }; auto map_it = word_count.cbegin(); while (map_it != word_count.cend()) {...}

3. 删除

参数返回值
m.erase(k)k为关键字 返回一个size_type,表示删除元素的数量
m.erase§p是迭代器 p必须指向m中一个真实元素,返回指向p之后元素的迭代器(可以为end)
m.erase(beg,end)beg end是迭代器 返回end

4. 下标操作

c[k]
c.at(k)
区别是什么?

5. 访问元素

方法返回值
c.find(k)返回一个迭代器,指向第一个关键字为k的元素,若没有找到则返回end
c.count(k)返回一个size_type,表示删除元素的数量
c.lower_bound(k)返回一个迭代器,指向第一个关键字不小于k的元素
c.upper_bound(k)返回一个迭代器,指向第一个关键字大于k的元素
c.equal_range(k)返回一个迭代器pair,表示关键字等于k的元素范围。若k不存在,pair的两个成员均为end()

eg.

// map from author to title; there can be multiple titles per author multimap<string, string> authors;// add data to authors authors.insert({ "Alain de Botton", "On Love" }); authors.insert({ "Alain de Botton", "Status Anxiety" }); authors.insert({ "Alain de Botton", "Art of Travel" }); authors.insert({ "Alain de Botton", "Architecture of Happiness" }); /* authors.insert(pair<string, string>("Alain de Botton", "On Love")); authors.insert(pair<string, string>("Alain de Botton", "Status Anxiety")); authors.insert(pair<string, string>("Alain de Botton", "Art of Travel")); authors.insert(pair<string, string>("Alain de Botton", "Architecture of Happiness")); */ string search_item("Alain de Botton"); // author we'll look for auto entries = authors.count(search_item); // number of elements auto iter = authors.find(search_item); // first entry for this author /* // loop through the number of entries there are for this author while (iter != authors.end()) {//while(entries) cout << iter->second << endl; // print each title++iter; // advance to the next title--entries; // keep track of how many we've printed } */ // definitions of authors and search_item as above // beg and end denote the range of elements for this author for (auto beg = authors.lower_bound(search_item),end = authors.upper_bound(search_item);beg != end; ++beg)cout << beg->second << endl; // print each title// definitions of authors and search_item as above // pos holds iterators that denote the range of elements for this key for (auto pos = authors.equal_range(search_item);pos.first != pos.second; ++pos.first)cout << pos.first->second << endl; // print each title【引用】1. 代码 https://github.com/thefistlei/cplusprimer/blob/main/cprimer/cprimer/mapTest.h

总结

以上是生活随笔为你收集整理的C++ Primer 5th笔记(chap 11)关联容器操作的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。