C++ Primer 5th笔记(chap 11)关联容器操作
生活随笔
收集整理的這篇文章主要介紹了
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)关联容器操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ Primer 5th笔记(cha
- 下一篇: C++ Primer 5th笔记(cha